在不保存新图像的情况下将图像调整大小并将其压缩为字节数组 [英] Resize and Compress image to byte array without saving the new image

查看:23
本文介绍了在不保存新图像的情况下将图像调整大小并将其压缩为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图像首先被调整大小,压缩,然后作为Preview.jpg"保存在磁盘上,然后打开它转换为字节数组.代码工作正常,但如果不将图像保存在磁盘上,我无法弄清楚如何做到这一点.

The image is first resized, compressed and then saved on disk as "Preview.jpg" and then it is opened to convert into byte array. The code works fine but I cannot figure out how to do it without saving the image on disk.

代码如下:

Public Function GetThumb_Preview(ByVal sourceImg As String) As Byte()

    Dim jgpEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
    Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
    Dim myEncoderParameters As New EncoderParameters(1)
    Dim myEncoderParameter As New EncoderParameter(myEncoder, 50&)
    myEncoderParameters.Param(0) = myEncoderParameter

    Dim myBitmap As New Bitmap(sourceImg)

    Dim oWidth As Integer = myBitmap.Width
    Dim oHeight As Integer = myBitmap.Height

    Dim aspectRatio As Double = oHeight / oWidth

    Dim thumbWidthDouble As Double = 200
    Dim thumbHeightDouble As Double = Math.Round(thumbWidthDouble * aspectRatio)

    Dim thumbWidth As Integer = CInt(thumbWidthDouble)
    Dim thumbHeight As Integer = CInt(thumbHeightDouble)

    Dim myThumb As New Bitmap(myBitmap, thumbWidth, thumbHeight)

    Dim targetPreviewPath As String = "E:\Preview.jpg"

    myThumb.Save(targetPreviewPath, jgpEncoder, myEncoderParameters)

    Dim myImage As Image = Image.FromFile(targetPreviewPath)

    Dim imgByteArray As Byte() = Nothing

    'Image to byte[]      
    Dim imgMemoryStream As MemoryStream = New MemoryStream()
    myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)
    imgByteArray = imgMemoryStream.GetBuffer()

    Return imgByteArray

End Function

推荐答案

那就是变数多,做的少.您还有许多未处理的对象.只有当生成的字节数据小于缓冲区大小时,它才会返回一个有效的字节数组.它在这种情况下有效,因为您将大小调整为 200 倍并降低质量.

That is a lot of variables to do so little. You also have a number of objects not being disposed. It will also return a valid Byte array only when the resulting byte data is less than the buffer size. It works in this case because you are resizing to 200x and reducing quality.

我没有测试我是否正确折叠了所有这些变量,但应该非常接近.更重要的是处理你创建的东西,并获得所有的字节返回:

I didnt test if I collapsed all those variables correctly, but should be very close. More important is disposing things you create, and getting the all the Bytes for the return:

Dim jgpEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
Dim myEncoder As System.Drawing.Imaging.Encoder = 
          System.Drawing.Imaging.Encoder.Quality
Dim myEncoderParameters As New EncoderParameters(1)
Dim myEncoderParameter As New EncoderParameter(myEncoder, 50&)
myEncoderParameters.Param(0) = myEncoderParameter

Dim imgByteArray As Byte()

Using myBitmap As New Bitmap(sourceImg)    ' i guess this is from file

    Dim aspectRatio As Double = myBitmap.Height / myBitmap.Width

    ' USING for disposable objects
    Using myThumb As New Bitmap(myBitmap, 200, 
                                CInt(Math.Round(200 * aspectRatio))),
        ms As New MemoryStream

        ' encode image to memstream
        myThumb.Save(ms, jgpEncoder, myEncoderParameters)

        ' rewind and get ALL bytes for the new image
        ms.Position = 0
        imgByteArray = ms.ToArray

    End Using
End Using           ' dispose

Return imgByteArray

这篇关于在不保存新图像的情况下将图像调整大小并将其压缩为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆