2012-05-09 3 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 X 200 비트 맵 내에서 이미지를 중심하려는 경우 또한 당신은 여기

0 내가 고체 브러시를 변경 한 것을 코드 (주이며, 대신 0을 전달하는 oGraphic.DrawImage에 y를 계산 X에 합격해야합니다 선명한 레드에 색상, 귀하의 경우 흰색에 그 변경해야합니다)

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

감사합니다 :) 당신은 환영합니다. –

관련 문제