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. 绘制并居中上传到中心的图像

我的问题是我无法保存创建的图像。这是转换问题吗?我已经突出显示了我认为我失败的代码。

4

1 回答 1

1

您将需要保存 oBitmap。此外,如果您想在 300 x 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

输出图像看起来像

在此处输入图像描述

于 2012-05-09T14:16:15.903 回答