Error converting byte to ImageSource - c#

Want to convert byte[] to Imagesource
here is my code for convert to byte
public object BufferFromImage(System.Windows.Media.ImageSource imageSource)
{
if (imageSource != null)
{
var image = (BitmapSource)imageSource;
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
using (var ms = new MemoryStream())
{
encoder.Save(ms);
return ms.ToArray();
}
}
else
{
return DBNull.Value;
}
}
code for byte[] to Imagesource
public ImageSource ByteToImage(byte[] imageData)
{
BitmapImage biImg = new BitmapImage();
MemoryStream ms = new MemoryStream(imageData);
biImg.BeginInit();
biImg.StreamSource = ms;
biImg.EndInit();
ImageSource imgSrc = biImg as ImageSource;
return imgSrc;
}
This is giving me this error:
An unhandled exception of type 'System.NotSupportedException' occurred
in PresentationCore.dll
Additional information: No imaging component suitable to complete this
operation was found.
What is causing this and how can I fix it?

Looks like you can save it to a MemoryStream then convert the MemoryStream to a byte array.try to this
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray
}
//second
public byte[] imgToByteArray(Image img)
{
using (MemoryStream mStream = new MemoryStream())
{
img.Save(mStream, img.RawFormat);
return mStream.ToArray();
}
}

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:
iVBORw0KGgoAAAANSUhEUgAAAIwAAACMCAIAAAAhotZpAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAACk0SURBVHhe7d1rjKVVuSfwql33a1d3V99o+gJNA7bQNAgeETRxJIQELxgTvOFBHUcP5ggzx8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NbprSnTxsxa3z3379tFYi33W3NwoXU9PzzhZiXZ0tC1Z0uVy5M9jfK6hVB8bT/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+aNJHpQCFVcIDM89NBDSKJ3W3snw1hFaWmNmDpoZDwjnbdy+rZt29atWxev6DHhCThHkgJa4vKSoziR/mst9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QX5gup3T/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.

How can i clear the memory after converting BitmapImage to Byte

I have two functions: one to convert from image to byte and other to convert from byte to bitmapImage.
So, when I open the window with that images, I convert from byte to bitmapImage and it works great, but when I close and open it again it just keeps on memory and if I continue to do that time and time again it just throws an exception Out Of Memory exception
Image to byte->
private byte[] ConvertImageToBinary(Image img)
{
using (MemoryStream ss = new MemoryStream())
{
img.Save(ss, System.Drawing.Imaging.ImageFormat.Jpeg);
var s = ss.ToArray();
var jpegQuality = 50;
Image image;
using (var inputStream = new MemoryStream(s))
{
image = Image.FromStream(inputStream);
var jpegEncoder = System.Drawing.Imaging.ImageCodecInfo.GetImageDecoders()
.First(c => c.FormatID == System.Drawing.Imaging.ImageFormat.Jpeg.Guid);
var encoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
encoderParameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, jpegQuality);
Byte[] outputBytes;
using (var outputStream = new MemoryStream())
{
image.Save(outputStream, jpegEncoder, encoderParameters);
return outputBytes = outputStream.ToArray();
}
}
}
}
Byte to bitmap ->
public BitmapImage ConvertBinaryToImage(byte[] array)
{
var image = new BitmapImage();
using (var ms = new MemoryStream(array))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad; // here
image.StreamSource = ms;
image.EndInit();
image.Freeze();
}
return image;
}
When I open the WindowDragAndDrop it loads all the images
But when I close it it still uses the same amount of memory
Image is indeed disposable (https://learn.microsoft.com/en-us/dotnet/api/system.drawing.image?view=netframework-4.8), so you also need:
using (var image = Image.FromStream(inputStream)){
}
Around everywhere you use Image objects.

Bitmap from Memstream of an SVG gives invalid parameter exception

The last line of the following code gives the error "Parameter is not valid", when the original image is an SVG:
var imageBytes = Convert.FromBase64String(imageBase64String);
var memStream = new MemoryStream(imageBytes);
//memStream.Seek(0, SeekOrigin.Begin);
var imageObject = new Bitmap(memStream);
Help please. Thanks.
EDIT: The image for example I am using is the image of the first formula in the following page right under the Theoremsection:
https://en.wikipedia.org/wiki/Green%27s_theorem
Can you try with 'using' for memorystream to handle garbage collection by itself?
var imageBytes = Convert.FromBase64String(imageBase64String);
Bitmap m = ByteToBitmap(imageBytes);
public static Bitmap ByteToBitmap(byte[] imageByte)
{
using (MemoryStream mStream = new MemoryStream())
{
mStream.Write(imageByte, 0, imageByte.Length); // this will stream dataand handle image length by itself
mStream.Seek(0, SeekOrigin.Begin);
Bitmap bm = new Bitmap(mStream);
return bm;
}
}
For SVG,
public static Bitmap ByteToBitmap(byte[] imageByte)
{
using (MemoryStream mStream = new MemoryStream(imageByte))
{
var s= SvgDocument.Open(mStream);
var bm= svgDocument.Draw();
return bm;
}
}

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();

Load bitmapImage from base64String

How can I load a bitmapImage from base64String in windows 8?
I tried this but I am not successful. It used to work on windows phone. What is different?
Looks like I have to use the function setsourceasync. When I use that, then I am required to pass the parameter as IRandomMemory which I am unable to do. How to do this?
public static BitmapImage Base64ToImage(string base64String)
{
var bitmapImage = new BitmapImage();
try
{
if (!String.IsNullOrEmpty(base64String))
{
var imageBytes = Convert.FromBase64String(base64String);
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
bitmapImage.SetSourcec(ms);
return bitmapImage;
}
}
}
catch (Exception e)
{
}
return null;
}
To create an IRandomAccessStream object for the SetSource method, you need to use a DataWriter. Take a look to this code:
public async Task<BitmapImage> GetImage(string value)
{
if (value == null)
return null;
var buffer = System.Convert.FromBase64String(value);
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes(buffer);
await writer.StoreAsync();
}
var image = new BitmapImage();
image.SetSource(ms);
return image;
}
}
Here conversion methods for both System.Drawing.Bitmap and System.Windows.Media.BitmapSource.
Enjoy
Remark: Not tested on Win8 but there is not reason why it should not work.
string ToBase64(Bitmap bitmap)
{
if (bitmap == null)
throw new ArgumentNullException("bitmap");
using (var stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Png);
return Convert.ToBase64String(stream.ToArray());
}
}
string ToBase64(BitmapSource bitmapSource)
{
using (var stream = new MemoryStream())
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
encoder.Save(stream);
return Convert.ToBase64String(stream.ToArray());
}
}
Bitmap FromBase64(string value)
{
if (value == null)
throw new ArgumentNullException("value");
using (var stream = new MemoryStream(Convert.FromBase64String(value)))
{
return (Bitmap)Image.FromStream(stream);
}
}
BitmapSource BitmapSourceFromBase64(string value)
{
if (value == null)
throw new ArgumentNullException("value");
using (var stream = new MemoryStream(Convert.FromBase64String(value)))
{
var decoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
BitmapSource result = decoder.Frames[0];
result.Freeze();
return result;
}
}

Categories