加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WPF Image to byte []

发布时间:2020-12-15 06:20:09 所属栏目:百科 来源:网络整理
导读:我正在尝试从System. Windows.Controls.Image转换为byte [],我不知道Image类中的哪个方法可以帮助这个场景,我的方式真的不知道该怎么做,因为在我的LINQ模型该字段显示为二进制类型,如果我想要保存它像一个byte []类型,我必须改变这个? 我发现这里贴了代码,
我正在尝试从System. Windows.Controls.Image转换为byte [],我不知道Image类中的哪个方法可以帮助这个场景,我的方式真的不知道该怎么做,因为在我的LINQ模型该字段显示为二进制类型,如果我想要保存它像一个byte []类型,我必须改变这个?

我发现这里贴了代码,但没有使用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
};

解决方法

真正的解决方案…如果要在System.Windows.Control.Image中保存jpg图像,当您的ORM上的数据库映射字段为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();
}

称为:

getJPGFromImageControl(firmaUno.Source as BitmapImage)

希望帮助:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读