将 PNG 字节数组转换为 JPEG 字节数组

本文关键字:数组 字节 字节数 JPEG 转换 PNG | 更新日期: 2023-09-27 18:30:51

我使用以下代码将 BitmapSource 转换为表示 png 的字节数组:

    /// <summary>
    /// Converts BitmapSource to a PNG Bitmap.
    /// </summary>
    /// <param name="source">The source object to convert.</param>
    /// <returns>byte array version of passed in object.</returns>
    public static byte[] ToPngBytes(this BitmapSource source)
    {
        // Write the source to the bitmap using a stream.
        using (MemoryStream outStream = new MemoryStream())
        {
            // Encode to Png format.
            var enc = new Media.Imaging.PngBitmapEncoder();
            enc.Frames.Add(Media.Imaging.BitmapFrame.Create(source));
            enc.Save(outStream);
            // Return image bytes.
            return outStream.ToArray();
        }
    }

我希望执行相同的操作,但转换一个 Jpeg 字节数组,而无需先创建 BitmapSource。

签名应如下所示:

public static byte[] ToPngBytes(this byte[] jpegBytes)

这段代码有效,但似乎效率低下,因为我必须使用可写位图来执行此操作:

    private WriteableBitmap colorBitmap;
    private byte[] GetCompressedImage(byte[] imageData, System.Windows.Media.PixelFormat format, int width, int height, int bytesPerPixel = sizeof(Int32))
    {
        // Initialise the color bitmap converter.
        if (colorBitmap == null)
            colorBitmap = new WriteableBitmap(width, height, 96.0, 96.0, format, null);
        // Write the pixels to the bitmap.
        colorBitmap.WritePixels(new Int32Rect(0, 0, width, height), imageData, width * bytesPerPixel, 0);
        // Memory stream used for encoding.
        using (MemoryStream memoryStream = new MemoryStream())
        {
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            // Add the frame to the encoder.
            encoder.Frames.Add(BitmapFrame.Create(colorBitmap));
            encoder.Save(memoryStream);
            // Get the bytes.
            return memoryStream.ToArray();
        }
    }

将 PNG 字节数组转换为 JPEG 字节数组

下面是一段代码,用于获取任何格式的字节数组并将其转换为具有JPG格式的字节数组:

byte[] jpgImageBytes = null;
using (var origImageStream = new MemoryStream(image))
using (var jpgImageStream = new MemoryStream())
{
    var jpgImage = System.Drawing.Image.FromStream(origImageStream);
    jpgImage.Save(jpgImageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    jpgImageBytes = jpgImageStream.ToArray();
    jpgImage.Dispose();
}

答案有点晚了,但这应该对其他人有所帮助。在此解决方案中,它是哪种类型的图像并不重要(只要它是图像,您就可以将其转换为任何类型。

public static byte[] ConvertImageBytes(byte[] imageBytes, ImageFormat imageFormat)
        {
            byte[] byteArray = new byte[0];
            FileStream stream = new FileStream("empty." + imageFormat, FileMode.Create);
            using (MemoryStream ms = new MemoryStream(imageBytes))
            {
                stream.Write(byteArray, 0, byteArray.Length);
                byte[] buffer = new byte[16 * 1024];
                int read;
                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                byteArray = ms.ToArray();
                stream.Close();
                ms.Close();
            }
            return byteArray;
        }

像下面这样使用它

ConvertImageBytes(imageBytes, ImageFormat.Png);