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

时间:2013-09-11 09:04:25

标签: c# arrays image-processing encoding bytearray

我使用以下代码将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();
        }
    }

2 个答案:

答案 0 :(得分:2)

下面是一段代码,用于获取任何格式的字节数组,并将其转换为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();
}

答案 1 :(得分:2)

迟到的答案,但这应该有助于其他人。在这个解决方案中,它与哪种类型的图像无关(只要它是一个图像,你可以将它转换为任何类型。

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);