Using PERL,VB.NET,JAVASCRIPT to Resize pictures

 

    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

'-------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值