Using PERL,VB.NET,JAVASCRIPT to Resize pictures
来源:互联网 发布:js访问style标签样式 编辑:程序博客网 时间:2024/06/05 17:30
When I develop projects , often need to resize an image to fit different space, while you directly use html tag like this "< img src="xxx" width="100" height="100" / >" ,the photo will be crashed, so we need to resize images in another way. Here are the code I wrote(just for a reference):
PERL(using Image::Magick module):
#-------------------------------------------------------
#begin
sub ResizeImage{
my $img = shift; #file name "tmp.jpeg"
my $base_width = 120;
my $base_height = 120;
my $image = Image::Magick->new($img);
my($width,$height) = $image->Get('width','height');
if($width<=$base_width && $height<=$base_height){
# don't resize
}
else{
$W_rate = $base_width/$width;
$H_rate = $base_height/$height;
if($W_rate >= $H_rate){$RATE = $H_rate;}
else{$RATE = $W_rate;}
$width = int($RATE*$width + 0.5);
$height = int($RATE*$height + 0.5);
}
$image->Scale(width => $width, height => $height);
$image->Sharpen('0.0x1.0');
$image->Write('-',quality => 70);
undef $image;
}
#end
#----------------------------------------------------
Javascript:
//----------------------------------------------------
//begin
function ResizeImage(id,w,h){
//<img src="tmp.jpeg" id="tmpid" alt="" />
var sw = document.getElementById(id).width;
var sh = document.getElementById(id).height;
var rate;
var width,height;
if ((sw<=w)&&(sh<=h)){return;}
else{
var w_rate = w/sw;
var h_rate = h/sh;
if(w_rate>=h_rate){rate = h_rate;}
else rate = w_rate;
width = Math.floor(rate * sw + 0.5);
height = Math.floor(rate * sh +0.5);
}
document.getElementById(id).width = width;
document.getElementById(id).height = height;
}
//end
//---------------------------------------------------------------
'------------------------------------------------------------------
'VB.NET Output directly to webpage
Private Sub ResizeImg(ByVal ImageBytes As Byte(), ByVal w As Integer, ByVal h As Integer)
'ImageBytes is read from MSSQLServer
'w is default width
'h is default height
Dim ms As New IO.MemoryStream(ImageBytes)
Dim oms As New IO.MemoryStream
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(ms)
Dim width As Int32 = image.Width
Dim height As Int32 = image.Height
Dim rate As Double
If width <= w And height <= h Then
Else
Dim w_rate As Double = w / width
Dim h_rate As Double = h / height
If w_rate >= h_rate Then
rate = h_rate
Else
rate = w_rate
End If
width = CInt(Math.Round(rate * width + 0.5))
height = CInt(Math.Round(rate * height + 0.5))
End If
'Dim bitImage As New System.Drawing.Bitmap(wi, hi)
Dim bitImage As New System.Drawing.Bitmap(width, height)
Dim g As System.Drawing.Graphics = Drawing.Graphics.FromImage(bitImage)
g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.High
g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
g.Clear(Color.Transparent)
g.DrawImage(image, New Rectangle(0, 0, width, height))
' g.DrawImage(image, New Rectangle(0, 0, wi, hi), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel)
Try
bitImage.Save(oms, System.Drawing.Imaging.ImageFormat.Jpeg)
Catch ex As Exception
Finally
image.Dispose()
bitImage.Dispose()
g.Dispose()
End Try
Dim nImageByte(oms.Length) As Byte
'oms.Read(nImageByte, 0, nImageByte.Length)
nImageByte = oms.GetBuffer()
Response.ContentType = "image/jpeg"
Response.AppendHeader("Content-Disposition", "filename=S_" & tid.ToString & rid.ToString & ".jpg")
Response.BinaryWrite(nImageByte)
End Sub
'end
'-------------------------------------------------------------------
- Using PERL,VB.NET,JAVASCRIPT to Resize pictures
- vb.net的using用法
- LINQ 首部曲 : LINQ To Object Part 1 - Using VB.NET 2008
- LINQ首部曲: LINQ To Object Part 2 - Using VB.NET
- C# to VB.net
- HTMLContentParser ASP.NET Project using VB.NET
- 【VB/.NET】Converting VB6 to VB.NET
- four pictures to explain DOM
- Retrieving Environmental Details using VB.NET
- Using System.Configuration.ConfigurationManager Example (VB.NET)
- c# to VB.net or Vb.net to C# 转换
- pictures
- Pictures
- C# to VB.Net translator..
- C# to VB.Net translator..
- VB.Net to C#实践
- VB.NET to C# Converter
- C# Convert to VB.NET
- Hadoop and Linux kernel 2.6.27 - epoll limits (java.io.IOException:Too many open files)
- Python应用第一波:文件打包备份
- 货物移动,MB31,MB1A等
- Windows2003下安装MySQL时出现的鉴权错误
- 订单确认bapi co15
- Using PERL,VB.NET,JAVASCRIPT to Resize pictures
- sscanf() - 从一个字符串中读进与指定格式相符的数据.
- DB2 账户及用户标识
- EXCEL中从单元格中取日期截止到月份的公式及示例
- 利用存储过程实现交叉表格式数据查询的一种通用方法
- struts使用总结一:慎用actionServlet成员变量,actionServlet生命周期,actionForm生命周期,struts上传注意事项
- ASP.Net中自定义Http处理及应用之HttpModule篇
- datatable常用方法
- wince5编译SDK出错的解决办法