编译器可以';在WP7中找不到JpegBitmapEncoder类

本文关键字:WP7 找不到 JpegBitmapEncoder 编译器 | 更新日期: 2023-09-27 18:29:22

我正在失去它,我做错了什么?

Error 3 The name 'BitmapFrame' does not exist in the current context

Error 2 The type or namespace name 'JpegBitmapEncoder' could not be found (are you missing a using directive or an assembly reference?)

代码:

namespace Microsoft.Samples.CRUDSqlAzure.Phone.Converters
{
using System;
using System.Windows.Data;
using System.IO;
using System.Windows.Media.Imaging;
public class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        byte[] rawImageBytes = (byte[])value;
        BitmapImage imageSource = null;
        try
        {
            using (MemoryStream stream = new MemoryStream(rawImageBytes))
            {
                stream.Seek(0, SeekOrigin.Begin);
                BitmapImage b = new BitmapImage();
                b.SetSource(stream);
                imageSource = b;
            }
            return imageSource;
        }
        catch 
        {
            return null;
        }

    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            BitmapImage bitmapImage = (BitmapImage)value;
            byte[] data;
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
            using (MemoryStream ms = new MemoryStream())
            {
                encoder.Save(ms);
                data = ms.ToArray();
            }
            return data;
        }
        catch
        {
            return null;
        }
    }
}

}

编译器可以';在WP7中找不到JpegBitmapEncoder类

wp7上的Silverlight没有JpegBitmapEncoder。如果您想将BitmapSource转换为字节数组,可以使用WriteableBitmap的SaveJpeg方法:
try
{
    BitmapImage bitmapImage = (BitmapImage)value;
    byte[] data;
    WriteableBitmap wb = new WriteableBitmap(bitmapImage);
    using (MemoryStream ms = new MemoryStream())
    {
        wb.SaveJpeg(ms, bitmapImage.PixelHeight, bitmapImage.PixelWidth, 0, 100);
        data = ms.ToArray();
    }
    return data;
}
catch
{
    return null;
}

如果您想将BitmapSource转换为其他文件格式,如png或gif,则必须使用第三方库,如.NET图像工具。

但是在转换器中来回转换图像不是一个好主意。我甚至不认为你真的需要。你使用什么控件来修改BitmapSource?:''