图像到byte [],Convert和ConvertBack [英] Image to byte[], Convert and ConvertBack

查看:80
本文介绍了图像到byte [],Convert和ConvertBack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项服务,可以将网站上存储的图像转换为字节数组

  HttpWebRequest myRequest =(HttpWebRequest)WebRequest.Create ( URLTOIMAGE); 
myRequest.Method =GET;
HttpWebResponse myResponse =(HttpWebResponse)myRequest.GetResponse();
位图bmp =新位图(myResponse.GetResponseStream());
myResponse.Close();
ms = new MemoryStream();
bmp.Save(ms,ImageFormat.Bmp);

此代码返回一个存储在数据库(SQL Azure)中的字节数组。在我的Windows Phone应用程序中,我尝试转换此字节数组以在我的页面上显示它。

  public class BytesToImageConverter:IValueConverter 
{
公共对象转换(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化)
{
BitmapImage empImage = new BitmapImage();
empImage.SetSource(new MemoryStream((Byte [])value));
返回empImage;
}

公共对象ConvertBack(对象值,类型targetType,对象参数,
System.Globalization.CultureInfo文化)
{
抛出新的NotImplementedException( );
}
}

字节数组很受应用程序的欢迎,但是当我尝试执行SetSource时抛出异常。

  empImage.SetSource(new MemoryStream((Byte [])value) ); 
=> 异常未处理,请求不受支持

你能帮助我吗?这个代码有效:

  public class BytesToImageConverter:IValueConverter 
{
public object Convert(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
{
MemoryStream stream = new MemoryStream((Byte [])value);
WriteableBitmap bmp = new WriteableBitmap(173,173);
bmp.LoadJpeg(stream);
返回bmp;
}

公共对象ConvertBack(对象值,类型targetType,对象参数,
System.Globalization.CultureInfo文化)
{
抛出新的NotImplementedException( );
}
}

谢谢大家:)


I have a service that converts images stored on a website to byte array

                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("URLTOIMAGE");
                myRequest.Method = "GET";
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                Bitmap bmp = new Bitmap(myResponse.GetResponseStream());
                myResponse.Close();
                ms = new MemoryStream();
                bmp.Save(ms, ImageFormat.Bmp);

This code returns a byte array that I store in a database (SQL Azure). In my Windows Phone application, I try to convert this byte array to display it on my page.

public class BytesToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        BitmapImage empImage = new BitmapImage();
        empImage.SetSource(new MemoryStream((Byte[])value));
        return empImage;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                                System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The byte array is well received by the application, but an exception is thrown when I try to do a SetSource.

empImage.SetSource(new MemoryStream((Byte[])value));
=> "Exception was unhandled", The request is not supported

Can you help me? Thx

解决方案

This code works :

public class BytesToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        MemoryStream stream = new MemoryStream((Byte[])value);
        WriteableBitmap bmp = new WriteableBitmap(173, 173);
        bmp.LoadJpeg(stream);
        return bmp;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                                System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Thank you everyone :)

这篇关于图像到byte [],Convert和ConvertBack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆