如何使用JpegBitmapEncoder保存图像

如何使用JpegBitmapEncoder保存图像

本文介绍了如何使用JpegBitmapEncoder保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据图像编码示例我应该是能够使用来对图像另存为jpeg文件,但出现此编译错误:

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

我看不到方法(属性或方法)在Image中)以从Image获得 System.Uri
我想念什么?

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

图片xaml代码为

<Image Name="ColorImage"/>

SaveImage C#是

The SaveImage C# is

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

以下代码(主要来自)将640 x 480 RBG流传输到的速度为30 Fps(kinect 是RgbResolution640x480Fps30)。

The code below (taken mostly from the kinect-sdk) streams 640 x 480 RBG to a WriteableBitmap at 30 Fps (the kinect ColorImageFormat is 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);
}


推荐答案

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

这篇关于如何使用JpegBitmapEncoder保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:09