Converting Bitmapimage to Bitmap and Vice Versa

Converting BitmapImage to Bitmap and vice versa

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;
}

C# Convert BitMap into BitmapImage from Properties.Resources

I have now found a way to convert the Bitmap to bitmapSource / bitmapimage. It's a slightly different version than the versions of the Stackoverflow thread I linked in the OP:

private static BitmapSource Bitmap2BitmapImage(Bitmap bitmap)
{
IntPtr hBitmap = bitmap.GetHbitmap();
BitmapSource retval = null;

try
{
retval = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

return retval;
}

This returns a BitmapSource Object, which can be used to display an Image in the WPF image Element

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.

How to convert Bitmap to BitmapImage on UWP?

Do you know any other way to get file's Icon

In UWP platform, we could use GetThumbnailAsync to get file's Thumbnail, for image file we could get an empty file's thumbnail to avoid getting the image preview. You could use the following method directly.

public static class FileExtension
{
public async static Task<StorageItemThumbnail> GetFileIcon(this StorageFile file, uint size = 32)
{
StorageItemThumbnail iconTmb;
var imgType = new[] { "bmp", "gif", "jpeg", "jpg", "png" }.FirstOrDefault(ext => file.Path.ToLower().EndsWith(ext));
if (imgType != null)
{
var dummy = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("dummy." + imgType , CreationCollisionOption.ReplaceExisting); //may overwrite existing
iconTmb = await dummy.GetThumbnailAsync(ThumbnailMode.SingleItem, size);
}
else
{
iconTmb = await file.GetThumbnailAsync(ThumbnailMode.SingleItem, size);
}
return iconTmb;
}
}

Usage

var icon = await file.GetFileIcon();
var imgSource = new BitmapImage();
imgSource.SetSource(icon);
Myimage.Source = imgSource;

Converting BitmapImage to Bitmap (Windows Phone 8.1 using Accord.net Portable Library)

As discussed here, you will need to cast from WriteableBitmap before you apply any AForge/Accord image operations, and cast back to WriteableBitmap after the image processing operations are completed.

Also see this answer.

Convert RenderTargetBitmap to BitmapImage

Although it doesn't seem to be necessary to convert a RenderTargetBitmap into a BitmapImage, you could easily encode the RenderTargetBitmap into a MemoryStream and decode the BitmapImage from that stream.

There are several BitmapEncoders in WPF, the sample code below uses a PngBitmapEncoder.

var renderTargetBitmap = getRenderTargetBitmap();
var bitmapImage = new BitmapImage();
var bitmapEncoder = new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));

using (var stream = new MemoryStream())
{
bitmapEncoder.Save(stream);
stream.Seek(0, SeekOrigin.Begin);

bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
}

Convert Bitmap image to String format to send over network(LAN) and vice-versa

Try to convert it to a byte array:

public static byte[] ImageToByteArray(Image img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Close();

byteArray = stream.ToArray();
}
return byteArray;
}

I believe you can simply cast a Bitmap object to an Image object. So Image img = (Image)myBitmap; - then pass that into the method above.



Related Topics



Leave a reply



Submit