WPF图像为byte [] [英] WPF Image to byte[]

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

问题描述

我想从 System.Windows.Controls.Image 转换为字节[] 和我没有知道哪些方法从图像类可以在这个诗情帮助下,我真的不知道该怎么办的方式,因为在我的LINQ模型中的字段显示为二进制键入,我要改变这一点,如果我想将其保存像字节[] 键入<?/ p>

我发现code张贴在这里,但没有使用WPF:

 位图newBMP =新位图(originalBMP,newWidth,newHeight);
System.IO.MemoryStream流=新System.IO.MemoryStream();
newBMP.Save(流System.Drawing.Imaging.ImageFormat.Bmp);
PHJProjectPhoto myPhoto =新PHJProjectPhoto {
    ProjectPhoto = stream.ToArray()//&LT;&LT; ---这将您的流转换为一个byte []
    订购日期= DateTime.Now,
    ProjectPhotoCaption = ProjectPhotoCaptionTextBox.Text,
    专案编号= selectedProjectId
};


解决方案

真正的解决办法...如果想从一个System.Windows.Control.Image保存JPG图片时,你的ORM数据库映射的字段是字节[] /字节[] / Bynary

 字节公众[] getJPGFromImageControl(BitmapImage的imageC)
{
       MemoryStream的memStream =新的MemoryStream();
        JpegBitmapEn codeR EN codeR =新JpegBitmapEn codeR();
        EN coder.Frames.Add(BitmapFrame.Create(imageC));
        EN coder.Save(memStream);
        返回memStream.ToArray();
}

电话为:

  getJPGFromImageControl(firmaUno.Source为BitmapImage的)

的希望帮助:)

I'm trying to convert from System.Windows.Controls.Image to byte[] and I didnt know which method from Image class could help in this scenary, by the way I really dont know what should I do, cause in my LINQ model the field appears as Binary type, I have to change this if I want to save it like a byte[] type?

I found code posted here, but without using WPF:

Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
System.IO.MemoryStream stream = new System.IO.MemoryStream();
newBMP.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
PHJProjectPhoto myPhoto = new PHJProjectPhoto {
    ProjectPhoto = stream.ToArray(), // <<--- This will convert your stream to a byte[] 
    OrderDate = DateTime.Now, 
    ProjectPhotoCaption = ProjectPhotoCaptionTextBox.Text,
    ProjectId = selectedProjectId
};

解决方案

Real Solution... if want to save jpg images from an System.Windows.Control.Image when your database mapped field on your ORM is Byte[] / byte[] / Bynary

public byte[] getJPGFromImageControl(BitmapImage imageC)
{
       MemoryStream memStream = new MemoryStream();              
        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(imageC));
        encoder.Save(memStream);
        return memStream.ToArray();
}

call as :

getJPGFromImageControl(firmaUno.Source as BitmapImage)

Hopes helps :)

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

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