2013-01-15 204 views
0

使用VB.net重新調整圖像大小後的白色背景,如下所示。我試圖改變一些值,但失敗了。如何根據原始圖像比例調整圖像大小?

Dim FileToResize As String = Server.MapPath("~/images/" & filename & FileUpload1.FileName) 
Using originalBitmap As Bitmap = Bitmap.FromFile(FileToResize, True), newbmp As Bitmap = New Bitmap(200, 200) 
    Dim WidthVsHeightRatio = CDec(originalBitmap.Width)/CDec(originalBitmap.Height) 

    Using newg As Graphics = Graphics.FromImage(newbmp) 
     newg.SmoothingMode = Drawing2D.SmoothingMode.HighQuality 
     newg.Clear(Color.White) 

     If WidthVsHeightRatio = 1D Then 
      newg.DrawImage(originalBitmap, 0, 0, 200, 200) 
      newg.Save() 


     ElseIf WidthVsHeightRatio < 1D Then 'Image is taller than wider 

      newg.DrawImage(originalBitmap, New RectangleF(New PointF((100 - (200 * WidthVsHeightRatio)/2), 0), New SizeF(200 * WidthVsHeightRatio, 200.0F))) 
      newg.Save() 

     Else 'Image is wider than taller 

      Dim inverse As Double = Math.Pow(WidthVsHeightRatio, -1) 
      newg.DrawImage(originalBitmap, New RectangleF(New PointF(0, 100 - ((200 * inverse)/2)), New SizeF(200.0F, 200 * inverse))) 
      newg.Save() 
     End If 
    End Using 

    newbmp.Save(Server.MapPath("~/images/" & "_th_" & filename & FileUpload1.FileName), System.Drawing.Imaging.ImageFormat.Jpeg) 
    NewsItem.ImageLink = filename & FileUpload1.FileName 
    NewsItem.smallimage = "_th_" & filename & FileUpload1.FileName 
End Using 
+0

莫非你請張貼兩張圖片 - 之前和之後? – Neolisk

+0

究竟是什麼問題?這看起來應該成功地爲調整大小的圖像選擇大小和位置。 –

回答

0

我最近在爲電子商務網站動態生成圖像縮略圖方面做了一些研究。我開始自己生成一個位圖,然後調整大小等。在光盤上的圖像大小和質量出現問題後,我查看了http://imageresizing.net/,從此我一直沒有回頭。它可以生成從字節(圖像),溪流和體檢用一行代碼很快所有文件:

ImageBuilder.Current.Build(New MemoryStream(bImage), sImageLocation + sFullFileName, New  ResizeSettings("maxwidth=214&maxheight=238")) 

我肯定會推薦這個組件,而不是試圖重新發明輪子...

相關問題