Bitmapimage to Byte[]

BitmapImage to byte[]

To convert to a byte[] you can use a MemoryStream:

byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
using(MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}

Instead of the JpegBitmapEncoder you can use whatever BitmapEncoder you like as casperOne said.

If you are using MS SQL you could also use a image-Column as MS SQL supports that datatype, but you still would need to convert the BitmapImage somehow.

BitMapImage to byte and reverse

I'm not really sure what you're doing with the getter-setter.

For getting a byte array from a file, I would have this:

public static byte[] FileToByteArray(string fileName)
{
byte[] fileData = null;

using (FileStream fs = new File.OpenRead(fileName))
{
var binaryReader = new BinaryReader(fs);
fileData = binaryReader.ReadBytes((int)fs.Length);
}
return fileData;
}

And once you have the bytes from that function, you can use ConvertToBitMapImage() and assign that to the Image.Source, like this:

byte[] bytes = FileToByteArray(fileName);
YourImage.Source = ConvertToBitMapImage(bytes);

I'm not sure why you would need to convert a BitmapImage into bytes, but you should be able to call your function directly for that:

BitmapImage bImg = new BitmapImage();
bImg.Source = ConvertToBitMapImage(bytes);
byte[] bytes = ImageToByte(bImg); // these should be the same bytes as went in

Set it up like this and step through the code to see where it snags. It's possible the bytes might need encoding when you grab it in ImageToByte().

But also, you don't just then set the byte[] directly to the IMAGES or this.IMAGES.IMAGE, because that isn't an Image object, it's a byte[]. Without knowing what your IMAGES model construct looks like with its property types, it's a little vague to know what is being set to what and why, but this seems like a bad idea to over-complicate what you should just do with function calls as it is needed, without a getter-setter and an IMAGES model in the way.

BitmapImage to byte[] - C# web

Finally, it seems that, obviously, it missed some libraries but we are limited with our application, so we decided to recover our pictures by another way. Anyway, thank you all.

Convert BitmapImage to byte[]

I think that it can't be done on this platform. I change my project to Windows Phone Silverlight/8.0 and there is working everything.

 public static BitmapImage BytesToImage(byte[] bytes)
{
BitmapImage bitmapImage = new BitmapImage();
try
{
using (MemoryStream ms = new MemoryStream(bytes))
{
bitmapImage.SetSource(ms);
return bitmapImage;
}
}
finally { bitmapImage = null; }
}

public static byte[] ImageToBytes(BitmapImage img)
{
using (MemoryStream ms = new MemoryStream())
{
WriteableBitmap btmMap = new WriteableBitmap(img);
System.Windows.Media.Imaging.Extensions.SaveJpeg(btmMap, ms, img.PixelWidth, img.PixelHeight, 0, 100);
img = null;
return ms.ToArray();
}
}

How to get BitmapImage from byte array in several different ImageFormats

You shouldn't use classes from the WinForms System.Drawing namespace in a WPF application (like you do with Image.FromFile).

WPF provides its own set of classes to load and save bitmaps from Streams and URIs, and has built-in support for automatically detecting the format of a bitmap frame buffer.

Just create a BitmapImage or a BitmapFrame directly from a Stream:

public static BitmapSource BitmaSourceFromByteArray(byte[] buffer)
{
var bitmap = new BitmapImage();

using (var stream = new MemoryStream(buffer))
{
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
}

bitmap.Freeze(); // optionally make it cross-thread accessible
return bitmap;
}

or

public static BitmapSource BitmaSourceFromByteArray(byte[] buffer)
{
using (var stream = new MemoryStream(buffer))
{
return BitmapFrame.Create(
stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
}

Either method returns a BitmapSource, which is the base class of BitmapImage and BitmapFrame, and should be sufficient to deal with bitmaps in the rest of your application. E.g. the Source property of an Image control uses another base class, ImageSource, as property type.

Note also that when you load a BitmapSource from a Stream that is to be closed after loading, you have to set BitmapCacheOption.OnLoad. Otherwise the Stream must be kept open until the bitmap is eventually shown.


For encoding a BitmapSource you should be using a method like this:

public static byte[] BitmapSourceToByteArray(BitmapSource bitmap)
{
var encoder = new PngBitmapEncoder(); // or any other BitmapEncoder

encoder.Frames.Add(BitmapFrame.Create(bitmap));

using (var stream = new MemoryStream())
{
encoder.Save(stream);
return stream.ToArray();
}
}

Convert a BitmapImage to byte array in UWP

Since you start from image url, the only way I can figure out is to get the stream of the image. To do this, RandomAccessStreamReference.CreateFromUri() method is really useful.

Windows.Storage.Streams.IRandomAccessStream random = await Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri(new Uri("http://...")).OpenReadAsync();

Then we have to decode the stream in order to be able to read all pixels for later usage.

Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(random);
Windows.Graphics.Imaging.PixelDataProvider pixelData = await decoder.GetPixelDataAsync();

Finally you can access pixel buffer in such a way.

byte[] bytes = pixelData.DetachPixelData();

BitmapImage to byte array and vice versa

Solution:

Well, I have found the solution of this issue, it was that I was using BitmapPixelFormat.Rgba8 in PixelDataProvider (while fetching pixels data). Rather I should use BitmapPixelFormat.Bgra8.



Related Topics



Leave a reply



Submit