WPF: System.Windows.Interop.InteropBitmap to System.Drawing.Bitmap - wpf

Is there a way to convert System.Windows.Interop.InteropBitmap to System.Drawing.Bitmap?
Thank you

I was able to solve my problem using the code below. msg.ThumbnailSource contains System.Windows.Interop.InteropBitmap type of object
BitmapSource bmpSource = msg.ThumbnailSource as BitmapSource;
MemoryStream ms = new MemoryStream();
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmpSource));
encoder.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(ms);

You could take the pixels using CopyPixels and use the code from the Bitmap.Lock/Unlock documentation to pass the pixels to the Bitmap.

Related

Create Freezable image from non-Freezable image

I have a DrawingImage that uses DynamicResources for its colors. I want to then use that image in an animation, however you can't use objects that use DynamicResources in animations because they are not Freezable. I know I can't have its colors dynamically change during the animation, I don't care about that. I just want to create a version of this image which I can use in the animation. So basically have the image evaluate all its resources, then spit out a version of it that I can call Freeze on to use in the animation. Surely this must be possible? I must be able to, for instance, write this image out to disk? That must create a Freezable version of it, no? I've googled and searched StackOverflow but can find nothing useful... Any help would be much appreciated.
This was surprisingly difficult to dig up a solution to, but I seem to have found one. I made this handy extension, which will take my DrawingImage and encode it as a PNG via the PngBitmapEncoder, then write that to a memory stream, then load that into a BitmapImage which is Freezable and can be used in my animation. Whole lotta work for something so simple!
public static class DrawingExtensions
{
public static BitmapImage GetAsFreezableBitmapImage(this DrawingImage drawingImage)
{
return drawingImage?.Drawing?.GetAsFreezableBitmapImage();
}
public static BitmapImage GetAsFreezableBitmapImage(this Drawing drawing)
{
if (drawing == null)
return null;
var drawingVisual = new DrawingVisual();
using (var drawingContext = drawingVisual.RenderOpen())
{
drawingContext.PushTransform(new TranslateTransform(-drawing.Bounds.X, -drawing.Bounds.Y));
drawingContext.DrawDrawing(drawing);
}
var bitmap = new RenderTargetBitmap((int)drawing.Bounds.Width, (int)drawing.Bounds.Height, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(drawingVisual);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
var bitmapImage = new BitmapImage();
using (var stream = new MemoryStream())
{
encoder.Save(stream);
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = stream;
bitmapImage.DecodePixelHeight = bitmap.PixelHeight;
bitmapImage.DecodePixelWidth = bitmap.PixelWidth;
bitmapImage.EndInit();
}
return bitmapImage;
}
}

How can I save the picture on image control in wpf?

I have a simple wpf application WIA.My app has an image control...
I was wondering how can I save the scanned picture on my hard disk?
Depends on the type of the Image.Source, assuming that you have a BitmapSource as in the article it should be along those lines:
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)image.Source));
using (FileStream stream = new FileStream(filePath, FileMode.Create))
encoder.Save(stream);
If it's a System.Drawing.Image object then just call Save?

How to update image file binding to Image control?

My app includes a Image control which has binding to a disk image file. I some condition, the image file need be updated. But the updating can't be done because the image file is open and can not be overwritten. What should I do?
You can try to remove the binding, so the image will not be used by your program
than overwrite the image file
and than re-add the binding
i'm not sure about this, but it's worth a try
Now my solution is:
To use a converter to convert the image path into BitmapImage.
in the converter, load the image using a FileStream and copy the data into a MemoryStream and finally close the FileStream.
BitmapImage bmp = new BitmapImage();
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.BeginInit();
var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
var memStream = new MemoryStream();
memStream.SetLength(fileStream.Length);
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
memStream.Flush();
fileStream.Close();
bmp.StreamSource = memStream;
bmp.EndInit();
return bmp;

Binding Bitmapimge to Image in Wpf?

This is a simple Question (lets see)
I want to bind bitmap image to Image. For doing this in cs code u must write this line.
this.leftImage.Source = new BitmapImage(new Uri(#"C:\a.bmp"));
But I want make Binding from resources. Because In release time resources became part of project.exe file and if you make binding from file(Mean set Image.source with Image file address), you must always put image file in the same address(disaster programing) :)
One option is to get it from a resx file. You can do something similar to this. Assuming Images.resx contains a Left image bitmap.
leftImage.Source = ConvertBitmapToBitmapImage(Images.Left);
...
private BitmapImage ConvertBitmapToBitmapImage(Bitmap bitmap)
{
MemoryStream memoryStream = new MemoryStream();
bitmap.Save(memoryStream, ImageFormat.Png);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(memoryStream.ToArray());
bitmapImage.EndInit();
return bitmapImage;
}
With some more work, you can do this from XAML too.

Write WPF output to image file

Is there a way I can write the output of WPF say a canvas to a image file, jpg or the like.
I want to use WPF to create a background for me as I want to use the
BitmapEffects for a rectangle and also radius the corners.
I want to use the bitmap in a webpage.
Is this possible?
Malcolm
I have a blog post all about this here. Here's the code from the article:
Rect rect = new Rect(canvas.RenderSize);
RenderTargetBitmap rtb = new RenderTargetBitmap((int)rect.Right,
(int)rect.Bottom, 96d, 96d, System.Windows.Media.PixelFormats.Default);
rtb.Render(canvas);
//encode as PNG
BitmapEncoder pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create(rtb));
//save to memory stream
System.IO.MemoryStream ms = new System.IO.MemoryStream();
pngEncoder.Save(ms);
ms.Close();
System.IO.File.WriteAllBytes("logo.png", ms.ToArray());
Console.WriteLine("Done");

Resources