C# WPF图像到字节[]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/553611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Image to byte[]
提问by Jonathan Escobedo
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?
我试图从转换System.Windows.Controls.Image
到byte[]
,我不知道哪一种方法从图像类可以在这个写景帮助下,我真的不知道的方式是我应该做的,因为在我的LINQ模型中的字段将显示为Binary
类型,我必须改变如果我想将它保存为一种byte[]
类型?
I found code posted here, but without using WPF:
我发现这里发布的代码,但没有使用 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
};
采纳答案by Jonathan Escobedo
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
真正的解决方案......如果你想在你的 ORM 上的数据库映射字段是 Byte[] / byte[] / Bynary 时从 System.Windows.Control.Image 保存 jpg 图像
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 :)
希望有所帮助:)
回答by gix
I don't know how your Image is declared, but suppose we have this XAML declaration:
我不知道你的 Image 是如何声明的,但假设我们有这个 XAML 声明:
<Image x:Name="img">
<Image.Source>
<BitmapImage UriSource="test.png" />
</Image.Source>
</Image>
Then you can convert the contents of test.png to a byte-array like this:
然后你可以将 test.png 的内容转换成这样的字节数组:
var bmp = img.Source as BitmapImage;
int height = bmp.PixelHeight;
int width = bmp.PixelWidth;
int stride = width * ((bmp.Format.BitsPerPixel + 7) / 8);
byte[] bits = new byte[height * stride];
bmp.CopyPixels(bits, stride, 0);
回答by Jonathan Escobedo
public byte[] BufferFromImage(BitmapImage imageSource)
{
Stream stream = imageSource.StreamSource;
byte[] buffer = null;
if (stream != null && stream.Length > 0)
{
using (BinaryReader br = new BinaryReader(stream))
{
buffer = br.ReadBytes((Int32)stream.Length);
}
}
return buffer;
}
would be another way, but difference is this have less bytes[x] than first solution
将是另一种方式,但不同之处在于这比第一个解决方案具有更少的字节 [x]
回答by Beny
This works for me:
这对我有用:
MemoryStream stream = (MemoryStream)bitmapImage.StreamSource;
byte[] data = stream.ToArray();
回答by Andreas
You could also use BitmapSources's CopyPixels method
您还可以使用 BitmapSources 的 CopyPixels 方法
int stride = snapshot.PixelWidth * (snapshot.Format.BitsPerPixel / 8);
byte[] data = new byte[stride * snapshot.PixelHeight];
snapshot.CopyPixels(data, stride, 0);
var memoryStream = new MemoryStream(data);
回答by Vahidshirzadi
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
var of = new OpenFileDialog();
of.Filter = "Image files (*.png;*.jpeg)|*.png;*.jpeg|All files (*.*)|*.*";
var res = of.ShowDialog();
if (res.HasValue)
{
imgPreview.Source = new BitmapImage(new Uri(of.FileName));
var t = Utils.ConvertBitmapSourceToByteArray(imgPreview.Source as BitmapSource);
var d = Utils.ConvertBitmapSourceToByteArray(new BitmapImage(new Uri(of.FileName)));
var s = Utils.ConvertBitmapSourceToByteArray(imgPreview.Source);
var enc = Utils.ConvertBitmapSourceToByteArray(new PngBitmapEncoder(), imgPreview.Source);
//imgPreview2.Source = Utils.ConvertByteArrayToBitmapImage(enc);
imgPreview2.Source = Utils.ConvertByteArrayToBitmapImage2(enc);
//var i = 0;
}
else
{
MessageBox.Show("Select a currect file...");
}
}
}
/util.cs/
/ util.cs/
public class Utils
{
public static byte[] ConvertBitmapSourceToByteArray(BitmapEncoder encoder, ImageSource imageSource)
{
byte[] bytes = null;
var bitmapSource = imageSource as BitmapSource;
if (bitmapSource != null)
{
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
using (var stream = new MemoryStream())
{
encoder.Save(stream);
bytes = stream.ToArray();
}
}
return bytes;
}
public static byte[] ConvertBitmapSourceToByteArray(BitmapSource image)
{
byte[] data;
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}
return data;
}
public static byte[] ConvertBitmapSourceToByteArray(ImageSource imageSource)
{
var image = imageSource as BitmapSource;
byte[] data;
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}
return data;
}
public static byte[] ConvertBitmapSourceToByteArray(Uri uri)
{
var image = new BitmapImage(uri);
byte[] data;
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}
return data;
}
public static byte[] ConvertBitmapSourceToByteArray(string filepath)
{
var image = new BitmapImage(new Uri(filepath));
byte[] data;
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}
return data;
}
public static BitmapImage ConvertByteArrayToBitmapImage(Byte[] bytes)
{
var stream = new MemoryStream(bytes);
stream.Seek(0, SeekOrigin.Begin);
var image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.EndInit();
return image;
}
}
回答by Stanislav Kuzmich
I like encoders and decoders from the namespace: System.Windows.Media.Imaging
我喜欢命名空间中的编码器和解码器:System.Windows.Media.Imaging
public static class Extensions {
public static byte[] ToByteArray(this BitmapSource bitmapSource) {
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
using (var stream = new MemoryStream()) {
encoder.Save(stream);
return stream.ToArray();
}
}
public static BitmapSource ToBitmapSource(this byte[] bytes) {
using (var stream = new MemoryStream(bytes)) {
var decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default);
return decoder.Frames.First();
}
}
}
You can use it like this:
你可以这样使用它:
var bytes = bitmapSource.ToByteArray();
Or like this:
或者像这样:
var bitmapSource = bytes.ToBitmapSource();