WPF Byte array to BitmapImage - c#

I read all first page results of "Byte array to BitmapImage" and I fount
Byte array to BitmapImage WP
byte[] to BitmapImage in silverlight
the problem is that I code dosen't work for me and I get this error:
'System.Windows.Media.Imaging.BitmapImage' does not contain a
definition for 'SetSource' and no extension method 'SetSource'
accepting a first argument of type
'System.Windows.Media.Imaging.BitmapImage' could be found (are you
missing a using directive or an assembly reference?)
My main code is:
int stride = CoverPhotoBitmap.PixelWidth * 4;
int size = CoverPhotoBitmap.PixelHeight * stride;
byte[] CoverPhotoPixels = new byte[size];
CoverPhotoBitmap.CopyPixels(CoverPhotoPixels, stride, 0);
byte[] HiddenPhotoPixels = new byte[size];
HiddenPhotoBitmap.CopyPixels(HiddenPhotoPixels, stride, 0);
ResultPhotoBitmap = ByteArraytoBitmap(HiddenPhotoPixels);
and my method is:
public static BitmapImage ByteArraytoBitmap(Byte[] byteArray)
{
MemoryStream stream = new MemoryStream(byteArray);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
return bitmapImage;
}

The example you found appears to have been specific to Silverlight. The exception explains that the method you called(SetSource) does not exist. What you need to do is set the StreamSource.
public static BitmapImage ByteArraytoBitmap(Byte[] byteArray)
{
MemoryStream stream = new MemoryStream(byteArray);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
return bitmapImage;
}

Related

How to convert base64 string to BitmapImage?

I'm reading data from a blob in MySQL in Base64 String.
I want to convert this to a BitmapImage.
I've tried many ways, but without success.
Here is an example:
public static BitmapImage Base64ToBitmapImage(this string base64String)
{
byte[] binaryData = Convert.FromBase64String(base64String);
BitmapImage img = new BitmapImage();
img.BeginInit();
img.StreamSource = new MemoryStream(binaryData);
img.EndInit();
return img;
}
One example of blob:
iVBORw0KGgoAAAANSUhEUgAAAIwAAACMCAIAAAAhotZpAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAACk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nk56ZT8vseWlNZk8kkccvbagC4LS7QDimz371b1JrxZvMvFJphmInRKZRn8Fle1wNsh4Ou4vNbprSnTxsxa3z3379tFYi33W3NwoXU9PzzhZiXZ0tC1Z0uVy5M9jfK6hVB8bT/9G87yC9915VGyx+It2F6d0ic1+5p7+Mvcpudu5kp6zOkW1C5TIVPG4Y8eO3/zmN46ia665Rhg5gXBpFIbiI5gzTGunQ7h4HiCu425Drf7+fhpTMfust1wSRk6+9HV7ug6Js71792PFH1dgbnw8feYuItvast+28c95+sv8xd2pdjwaGxobpPG6emx592CLR43VF4T27L9P8dxzzz3wwAM33HDDww8/LKE988wzg4ODb3vb26688koHEoGIPObHJ3haTrnimSN98lqpniViYD7cabR9+3aRhBjZwBYT7CqP//JXGzdu3LJly8qVKTvjLIJGYFF91gNVOI0+c7bzRANPM8/wyLD4aG1Jl9scYqux0CAfuPTC7373u+985ztPPvmkC4JAcfRu2rTpPe95z1vf+lYUym9vetObbNAIo5UrV65atSoiSUvE3NwwbySBpPziiy/Sz47zlrBz506P//fxJxhw0UUX3XLLLe973/uuvvoqXhoaGunu7hRDcrq9Rj6Y09KUVU7GuZCUK6mSfXuS3vKixbR5xoN0Q8quppXHmZnx0TGB8rOf/ezBBx989tlnL7nkEhc2THjrsCN1se6jH/3oHXfcgQa2B3/xi+Lss7GEM/ns5w0wbySJcXtNvEt0XuUef/xxqcAedPezj/QyhtJbt2697bbbbrrpJtx0d3eF5yU6AmwTWJyUzXci8rRzVsiVzCveJKIOWSgXgqSJ0kQ8SnECSFgcOnTItnvkf3z36aefZlH8Jou7DWcRerQIlKNHj951113vf//7DffIZAyJofyrvzTnOYQRzDNJLgtiSLL+7ne/6yrh6jk8kv6lHwLI6AVKMyaLqqtl9lWr0iePY2PpS5dIDqfEnDiiXvxVqZp+MVrhRg8nguiJf0k/ODRok7388ssuAj//+c+Fzsre9B/tXLFihQNG9qY/huLlFGdeVA8cOPDxj3/8Xe96l172mg1D8SW6acFakJaeK+aNJHpQCFVcI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s2vXrqeeesoQjhYxBgbZvMmP//Qf/9O9994bcRYlsdgrocACxIIjaWKy1NyU/DWVklz6JLBQX5gup3T/5JNP3nXXXXwtyJwfenGAJDKiRHRu3LhRjjLW25ggICYBCkFXf8IqZL72X7/6sY99zCRBTHWZrb8Qcb4idM5I34RKI2rlmfRr5uxb2fJU+mnVTTe+/R033ax9SVc3MS+sq1asbGlqTu89eC3P9O3bv/2ll3dsf6X/8BG37RXLe4tj4y/9+cWhY4NLl/SY4cD+PjcCDAmgWC5w/jLVvGDBKSf1c6KKHBW7W6yLA5HEs3fccYc8FncBLSJD0uvv7+/L/oPXAkK6c6obq1EwufKJOVcDl2aZ87Of/axDyJxGxSrV5YLFgiOJ34F/gwAtUTo/pDJXZECPVKbRqe6W5XwSCobEDxN5nLBR6Fm9ejVq3fIlxltuueWBBx6IGyCZIEZpE0R9wWLBnUmnBI8LIGSIEkHz0EMPffvb3xZn3pBwsC77mRGZuF4KJpTElZJ1gsm5dffdd993332bN2/OJ8RrMBRiCznjLbyLw+w/m+FlCFd61BjhInqUDz/88BNPPPGTn/wEMbIfK3p7e43FBzpJahFtruYf+MAHIv5ctTEqE0ZpTlQFlzG5ysLEgo4kvgMkcWW+9x0t8S+5vQnJbzt27HjkkUd+8IMfuGQjLwZeffXVziHvpzfffLOrebyB4kYaZK+jK156crYWSTrvYELQqc7XwWU8noyFTMbpUHsa/xvEhUOS6IHKw4WFCyTdBdSDKojHk6GrUqsdXCAkVWqzWCRpEX9r1PyZdMpNdoHtvMXbXQ1gMd3VABYjqQawSFINYJGkGsAiSTWARZJqAIsk1QAWSaoBLJJUA1gkqQawSFINYJGkGsAiSTWARZJqAIsk1QAWSaoBLJJUA1gkqQawSFINYJGkBY+6uv8PydCKtZTj+VcAAAAASUVORK5CYII=
In my project, I used this method to convert an array of bytes to BitmapImage:
public static BitmapImage? ConvertToBitmapImage(byte[]? imageData)
{
if (imageData == null || imageData.Length == 0) return null;
var image = new BitmapImage();
using (var mem = new MemoryStream(imageData))
{
mem.Position = 0;
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = null;
image.StreamSource = mem;
image.EndInit();
}
image.Freeze();
return image;
}
You can use this function by passing an array of bytes to it:
public static BitmapImage? Base64ToBitmapImage(this string base64String)
{
byte[] binaryData = Convert.FromBase64String(base64String);
return ConvertToBitmapImage(binaryData);
}
you can use this functions.
private string ImageToBase64String(string file)
{
Bitmap bitmap = new Bitmap(file);
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
string strBase64 = Convert.ToBase64String(arr);
return strBase64;
}
private Bitmap Base64StringToImage(string base64)
{
byte[] arr = Convert.FromBase64String(base64);
MemoryStream ms = new MemoryStream(arr);
Bitmap bitmap = new Bitmap(ms);
return bitmap;
}
The code above works fine. The cause of the problem its on MySQL return.
The value from a blob column returns returns as a byte on C# MySQL Lybrary.
And I was converting it to a Base64 String and recoverting to a byte.
I excluded this confusion in my code and problem fixed.

Convert BitmapImage to byte[]

I have problem with converting BitmapImage to byte[]. I tried a lot of solutions and nothing works, every time i get different errors.
For example i found nice solutions but it also doesn't work. What's wrong with it?
I'm using Windows Phone 8.1.
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();
}
}
this was taken from here: Convert Bitmap Image to byte array (Windows phone 8)
There is no argument given that corresponds to the required formal
parameter 'pixelHeight' of 'WriteableBitmap.WriteableBitmap(int, int)'
The type or namespace name 'Extensions' does not exist in the
namespace 'System.Windows.Media.Imaging' (are you missing an assembly
reference?)
or if somebody has got another idea how to convert it, please post it. Thanks a lot for any help!
I also tried this: BitmapImage to byte[]
but there was problem with usings
'BitmapImage' is an ambiguous reference between 'System.Windows.Media.Imaging.BitmapImage' and 'Windows.UI.Xaml.Media.Imaging.BitmapImage'
so I used "BitmapEncoder" but it doesn't have method like Save and Frame.
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();
}
}
see the below link it might help
https://social.msdn.microsoft.com/Forums/en-US/713c0ed1-d979-43ef-8857-bbe0b35576a9/windows-8-how-to-convert-bitmapimage-into-byte?forum=winappswithcsharp
Have you tried
MemoryStream ms = new MemoryStream();
WriteableBitmap wb = new WriteableBitmap(myImage);
wb.SaveJpeg(ms, myImage.PixelWidth, myImage.PixelHeight, 0, 100);
byte[] imageBytes = ms.ToArray();
You can also use extension method here
public static class MyExtensions
{
public static Byte[] ByteFromImage(this System.Windows.Media.Imaging.BitmapImage imageSource)
{
Stream stream = imageSource.StreamSource;
Byte[] imagebyte = null;
if (stream != null && stream.Length > 0)
{
using (BinaryReader br = new BinaryReader(stream))
{
imagebyte = br.ReadBytes((Int32)stream.Length);
}
}
return imagebyte;
}
}
and then call
System.Windows.Media.Imaging.BitmapImage myImage = new System.Windows.Media.Imaging.BitmapImage();
byte[] imageBytes = myImage.ByteFromImage();
If you're going to do any transformation on the image, you'll want to use the appropriate image encoder, then do something like below. If you're working with a multi-frame image (eg TIF), you need to add the frames one at a time to the encoder or you'll only get the first frame of the image.
MemoryStream ms = null;
TiffBitmapEncoder enc = null
enc = new TiffBitmapEncoder();
enc.Compression = TiffCompressOption.Ccitt4;
enc.Frames.Add(BitmapFrame.Create(bmpImg));
using (ms = new MemoryStream())
{
enc.Save(ms);
}
return ms.ToArray();

Convert BitmapImage to byte in metro app

Like in the title. How to convert BitmapeImage to byte in metro style app. Ther is no System.drawing lib.
I need it to use in LuminanaceSource() which need source in byte[].
Have some example, but i dont know how to use it.
BitmapImage bmpImage = new BitmapImage(new Uri(file.Path));
var encoderId = Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId;
Thanks
these two methods should convert Bitmap image to byte[] and viceversa.
public BitmapImage ImageFromBuffer(Byte[] bytes)
{
MemoryStream stream = new MemoryStream(bytes);
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.EndInit();
return image;
}
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;
}

bitmapimage converting from byte array c#

I am trying to convert byte array, which contains image description to bitmapimage
but I got an error: "Additional information: No imaging component suitable to complete this operation was found."
My code is:
public BitmapImage ImageData
{
get
{
BitmapImage btm;
using (MemoryStream ms = new MemoryStream(mSpaceObject.ImageData))
{
btm = new BitmapImage();
btm.BeginInit();
btm.StreamSource = ms;
btm.CacheOption = BitmapCacheOption.OnLoad;
btm.EndInit();
btm.Freeze();
}
return btm;
}
}

Converting BitmapImage to Bitmap and vice versa

I have BitmapImage in C#. I need to do operations on image. For example grayscaling, adding text on image, etc.
I have found function in stackoverflow for grayscaling which accepts Bitmap and returns Bitmap.
So I need to convert BitmapImage to Bitmap, do operation and convert back.
How can I do this? Is this best way?
There is no need to use foreign libraries.
Convert a BitmapImage to Bitmap:
private Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage)
{
// BitmapImage bitmapImage = new BitmapImage(new Uri("../Images/test.png", UriKind.Relative));
using(MemoryStream outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapImage));
enc.Save(outStream);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);
return new Bitmap(bitmap);
}
}
To convert the Bitmap back to a BitmapImage:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
private BitmapImage Bitmap2BitmapImage(Bitmap bitmap)
{
IntPtr hBitmap = bitmap.GetHbitmap();
BitmapImage retval;
try
{
retval = (BitmapImage)Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
finally
{
DeleteObject(hBitmap);
}
return retval;
}
Here's an extension method for converting a Bitmap to BitmapImage.
public static BitmapImage ToBitmapImage(this Bitmap bitmap)
{
using (var memory = new MemoryStream())
{
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
return bitmapImage;
}
}
If you just need to go from BitmapImage to Bitmap it's quite easy,
private Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage)
{
return new Bitmap(bitmapImage.StreamSource);
}
using System.Windows.Interop;
...
private BitmapImage Bitmap2BitmapImage(Bitmap bitmap)
{
BitmapSource i = Imaging.CreateBitmapSourceFromHBitmap(
bitmap.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
return (BitmapImage)i;
}
I've just been trying to use the above in my code and I believe that there is a problem with the Bitmap2BitmapImage function (and possibly the other one as well).
using (MemoryStream ms = new MemoryStream())
Does the above line result in the stream being disposed of? Which means that the returned BitmapImage loses its content.
As I'm a WPF newbie I'm not sure that this is the correct technical explanation, but the code didn't work in my application until I removed the using directive.
Here the async version.
public static Task<BitmapSource> ToBitmapSourceAsync(this Bitmap bitmap)
{
return Task.Run(() =>
{
using (System.IO.MemoryStream memory = new System.IO.MemoryStream())
{
bitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
return bitmapImage as BitmapSource;
}
});
}
Thanks Guillermo Hernandez, I created a variation of your code that works. I added the namespaces in this code for reference.
System.Reflection.Assembly theAsm = Assembly.LoadFrom("My.dll");
// Get a stream to the embedded resource
System.IO.Stream stream = theAsm.GetManifestResourceStream(#"picture.png");
// Here is the most important part:
System.Windows.Media.Imaging.BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.StreamSource = stream;
bmi.EndInit();
This converts from System.Drawing.Bitmap to BitmapImage:
MemoryStream ms = new MemoryStream();
YOURBITMAP.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
BitmapImage image = new BitmapImage();
image.BeginInit();
ms.Seek(0, SeekOrigin.Begin);
image.StreamSource = ms;
image.EndInit();

Categories