wpf 如何使用 JpegBitmapEncoder 保存图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17549453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:10:57  来源:igfitidea点击:

How to save image using JpegBitmapEncoder

c#wpf.net-4.5jpegbitmapencoder

提问by Hymannad

According to the image encoding example hereI should be able to use JpegBitmapEncoderto encode an image for saving as a jpeg file but get this compile error:

根据这里的图像编码示例我应该能够使用JpegBitmapEncoder对图像进行编码以保存为 jpeg 文件,但出现此编译错误:

error CS1503: Argument 1: cannot convert from 'System.Windows.Controls.Image' to 'System.Uri'

错误 CS1503:参数 1:无法从“System.Windows.Controls.Image”转换为“System.Uri”

I don't see a way (property or method in Image) to get System.Urifrom Image. What am I missing?

我没有看到System.Uri从 Image 中获取的方法(Image 中的属性或方法)。我错过了什么?

The Image xaml code is

图像 xaml 代码是

<Image Name="ColorImage"/>

The SaveImage C# is

SaveImage C# 是

...
SaveImage(ColorImage, path);
...
private void SaveImage(Image image, string path)
{
    var jpegEncoder = new JpegBitmapEncoder();
    jpegEncoder.Frames.Add(BitmapFrame.Create(image));
    using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
    {
        jpegEncoder.Save(fs);
    }
}

The code below (taken mostly from the kinect-sdk) streams 640 x 480 RBG to a WriteableBitmapat 30 Fps (the kinect ColorImageFormatis RgbResolution640x480Fps30).

下面的代码(从大多采取的kinect-SDK)流640×480 RBG到WriteableBitmap的以30fps(Kinect的ColorImageFormat是RgbResolution640x480Fps30)。

using (var colorImageFrame = allFramesReadyEventArgs.OpenColorImageFrame())
{
    if (colorImageFrame == null) return;
    var haveNewFormat = currentColorImageFormat != colorImageFrame.Format;
    if (haveNewFormat)
    {
        currentColorImageFormat = colorImageFrame.Format;
        colorImageData = new byte[colorImageFrame.PixelDataLength];
        colorImageWritableBitmap = new WriteableBitmap(
            colorImageFrame.Width, 
            colorImageFrame.Height, 96, 96, PixelFormats.Bgr32, null);
        ColorImage.Source = colorImageWritableBitmap;
    }
    // Make a copy of the color frame for displaying.
    colorImageFrame.CopyPixelDataTo(colorImageData);
    colorImageWritableBitmap.WritePixels(
        new Int32Rect(0, 0, colorImageFrame.Width, colorImageFrame.Height),
        colorImageData,
        colorImageFrame.Width*Bgr32BytesPerPixel,
        0);
}

回答by Hymannad

private void SaveImage(string path)
{
    var jpegEncoder = new JpegBitmapEncoder();
    jpegEncoder.Frames.Add(BitmapFrame.Create(colorImageWritableBitmap));
    using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
    {
        jpegEncoder.Save(fs);
    }
}

回答by Romano Zumbé

The problem occurs, because you pass an Imageto BitmapFrame.Create. Imageis more common in Windows Forms. A simple approach would be to create a MemoryStreamfirst and the pass this:

出现问题,因为您传递了一个Imageto BitmapFrame.CreateImage在 Windows 窗体中更常见。一个简单的方法是创建MemoryStream第一个并传递:

private void SaveImage(Image image, string path)
    {
        MemoryStream ms = new MemoryStream();
        image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
        var jpegEncoder = new JpegBitmapEncoder();
        jpegEncoder.Frames.Add(BitmapFrame.Create(ms));
        using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
        {
            jpegEncoder.Save(fs);
        }
    }

Addition (see conversation in comments):

添加(见评论中的对话):

you could try to use the CopyPixelsmethod on the writeableBitmap object and copy the pixels to a byte array which you load to a MemoryStreamand the write it to a Jpeg File withe the JpegBitmapEncoder. But it's just a guess.

您可以尝试CopyPixels在 writeableBitmap 对象上使用该方法,并将像素复制到您加载到 a 的字节数组中,MemoryStream然后将其写入带有JpegBitmapEncoder. 但这只是一个猜测。

This could work, too:

这也可以:

     private void SaveImage (WriteableBitmap img, string path)
     {
         FileStream stream = new FileStream(path, FileMode.Create);
         JpegBitmapEncoder encoder = new JpegBitmapEncoder();
         encoder.Frames.Add(BitmapFrame.Create(img));
         encoder.Save(stream);
         stream.Close();
     }

You will just have to extract the WriteableBitmap from your Image control

您只需从 Image 控件中提取 WriteableBitmap

回答by jlew

Try: BitmapFrame.Create(image.Source)

尝试: BitmapFrame.Create(image.Source)