简体   繁体   English

编译器在WP7中找不到类JpegBitmapEncoder

[英]Compiler can't find the class JpegBitmapEncoder in WP7

i'm losing it, what am i doing wrong ? 我失去了它,我做错了什么?

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

Code: 码:

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

} }

Silverlight on wp7 doesn't have JpegBitmapEncoder. wp7上的Silverlight没有JpegBitmapEncoder。 If you want to convert a BitmapSource to a byte array you can do that using the WriteableBitmap's SaveJpeg method: 如果要将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;
}

If you want to convert the BitmapSource to other file format like png or gif you have to use third party libraries like .NET image tools . 如果要将BitmapSource转换为其他文件格式(如png或gif),则必须使用第三方库,如.NET图像工具

But it's not a good idea to convert images back and forth in a converter. 但是在转换器中来回转换图像并不是一个好主意。 I don't even think you really need to. 我甚至认为你真的不需要。 What control do you use which modifies the BitmapSource? 您使用什么控件来修改BitmapSource? :\\ :\\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM