问题描述
我正在尝试从 System.Windows.Controls.Image
转换为 byte[]
但我不知道 Image 类中的哪种方法可以帮助解决这个场景,顺便说一句,我真的不知道该怎么办,因为在我的 LINQ 模型中,该字段显示为 Binary
类型,如果我想将它保存为 byte[] ,我必须更改它
类型?
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?
我发现代码张贴在这里,但没有使用 WPF:
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
};
推荐答案
真正的解决方案...如果您想在 ORM 上的数据库映射字段为 Byte[]/时从 System.Windows.Control.Image 保存 jpg 图像byte[]/Bynary
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();
}
调用:
getJPGFromImageControl(firmaUno.Source as BitmapImage)
希望有所帮助:)
这篇关于WPF图像到字节[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!