2012-05-09 59 views
2

我有以下代碼:轉換位圖以字節

Private Function SizeLogo(ByVal logoBytes As Byte()) As Byte() 
     Using originalMemStream As MemoryStream = New MemoryStream(logoBytes), 
      originalImage As System.Drawing.Image = System.Drawing.Image.FromStream(originalMemStream) 

      Dim width As Integer = originalImage.Width 
      Dim height As Integer = originalImage.Height 
      Dim targetWidth As Integer = 300 
      Dim targetHeight As Integer = 200 
      Dim newHeight As Integer = 0 
      Dim newWidth As Integer = 0 

      Using oBitmap As New Bitmap(300, 200) 
       Using oGraphic As Graphics = Graphics.FromImage(oBitmap) 

        Using oBrush As New SolidBrush(Color.White) 
         oGraphic.FillRectangle(oBrush, 0, 0, targetWidth, targetHeight) 
        End Using 

        Using oProduto As System.Drawing.Image = Drawing.Image.FromStream(originalMemStream) 

         Dim targetRatio As Decimal = CDec(targetWidth)/CDec(targetHeight) 
         Dim imageRatio As Decimal = CDec(width)/CDec(height) 

         If targetRatio > imageRatio Then 
          newHeight = targetHeight 
          newWidth = CInt(Math.Floor(imageRatio * CDec(targetHeight))) 
         Else 
          newHeight = CInt(Math.Floor(CDec(targetWidth)/imageRatio)) 
          newWidth = targetWidth 
         End If 

         If newWidth > targetWidth Then newWidth = targetWidth 
         If newHeight > targetHeight Then newHeight = targetHeight 

         oGraphic.DrawImage(oProduto, 0, 0, newWidth, newHeight) 



         **Using thumbImage As Drawing.Image = oGraphic. .GetThumbnailImage(targetWidth, targetHeight, Nothing, IntPtr.Zero), 
         outMemStream As MemoryStream = New MemoryStream() 

          thumbImage.Save(outMemStream, ImageFormat.Jpeg) 
          Return outMemStream.ToArray() 
         End Using** 
        End Using 
       End Using 
      End Using 
     End Using 
    End Function 

我所試圖做的是

  1. 創建300width和200height白色位
  2. 繪製並上傳到中心圖像中心

我的問題是我無法保存創建的圖像。這是一個轉換問題嗎?我突出了代碼,我認爲我失敗了。

回答

1

您需要保存oBitmap。此外,如果你想將300×200的位圖中居中圖像,您將需要通過計算X,Y向oGraphic.DrawImage強似0,0

下面是代碼(注意,我已經改變了固體刷顏色爲紅色爲清楚起見,請務必將它改成白色爲你的情況)

Private Function SizeLogo(ByVal logoBytes As Byte()) As Byte() 
    Using originalMemStream As MemoryStream = New MemoryStream(logoBytes), 
     originalImage As System.Drawing.Image = System.Drawing.Image.FromStream(originalMemStream) 

     Dim width As Integer = originalImage.Width 
     Dim height As Integer = originalImage.Height 
     Dim targetWidth As Integer = 300 
     Dim targetHeight As Integer = 200 
     Dim newHeight As Integer = 0 
     Dim newWidth As Integer = 0 

     Using oBitmap As New Bitmap(300, 200) 
      Using oGraphic As Graphics = Graphics.FromImage(oBitmap) 

       Using oBrush As New SolidBrush(Color.Red) 
        oGraphic.FillRectangle(oBrush, 0, 0, targetWidth, targetHeight) 
       End Using 

       Using oProduto As System.Drawing.Image = Drawing.Image.FromStream(originalMemStream) 

        Dim targetRatio As Decimal = CDec(targetWidth)/CDec(targetHeight) 
        Dim imageRatio As Decimal = CDec(width)/CDec(height) 

        If targetRatio > imageRatio Then 
         newHeight = targetHeight 
         newWidth = CInt(Math.Floor(imageRatio * CDec(targetHeight))) 
        Else 
         newHeight = CInt(Math.Floor(CDec(targetWidth)/imageRatio)) 
         newWidth = targetWidth 
        End If 

        If newWidth > targetWidth Then newWidth = targetWidth 
        If newHeight > targetHeight Then newHeight = targetHeight 

        Dim x As Integer = 0 
        Dim y As Integer = 0 

        If (newWidth < targetWidth) Then 
         x = (targetWidth - newWidth)/2 
        End If 

        If (newHeight < targetHeight) Then 
         y = (targetHeight - newHeight)/2 
        End If 

        oGraphic.DrawImage(oProduto, x, y, newWidth, newHeight) 

        'uncomment this to save to file (just to test output) 
        'oBitmap.Save("D:\penguins.bmp") 

        Dim outMemStream As MemoryStream = New MemoryStream() 
        oBitmap.Save(outMemStream, ImageFormat.Jpeg) 
        Return outMemStream.GetBuffer() 

       End Using 
      End Using 
     End Using 
    End Using 
End Function 

輸出圖像看起來就像

enter image description here

+0

謝謝!你搖滾! –

+0

謝謝:)不客氣 –