Binding Bitmapimge to Image in Wpf? - 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.

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

Load image from another assembly's resx file in XAML

I have two assemblies, say assembly1 and assembly2.
In assembly2 there is a XAML file. In this XAML file I want to create an image.
What I want to do is setting the source of this image to a bitmap that is in a resx file from assembly1.
<Image Name="image1" Stretch="Fill" Source="???" />
How do I correctly reference to that bitmap file in XAML? Is there an easy "XAML-only" solution?
Okay, so I assume there is no thing such as an "XAML-only" solution.
Instead, I do it like this after the WPF control's Loaded event is called:
Assembly coreAssembly = Assembly.GetAssembly(typeof (otherAssembly.Resources));
var resourceManager = new ResourceManager("otherAssembly.Resources", coreAssembly);
// get image from core resources
Bitmap completeImage = (Bitmap) resourceManager.GetObject("Complete");
// apply image to WPF image
var memoryStream = new MemoryStream();
completeImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(memoryStream.ToArray());
bitmapImage.EndInit();
this.myWpfImage.Source = bitmapImage;

how to get BitmapImage in codebehind from the image tag in xaml in wpf/silverlight

i dont have a problem with binding a bitmapimage to image tag in codebehind for eg.
BitmapImage image = new BitmapImage();
imagetaginxaml.Source = image; // this will remove whatever image is currently on the image tag in xaml and attach the empty bitmapimage above
but i'm not able to get the image by doing the reverse, for example, i want to process the image that is currently on the image tag. i am not able to do this
BitmapImage image = imagetaginxaml.Source;
what should i do
Well, Image.Source is of type ImageSource, there is no quarantee that it will be a BitmapImage, it may be though. If the source is created by the XAML parser it will be a BitmapFrameDecode (which is an internal class). Anyway, the only save assignment is:
ImageSource source = img.Source;
otherwise you need to cast:
BitmapImage source = (BitmapImage)img.Source;
which will throw an exception if the Source is not of this type. So you can either save-cast or try-catch:
//(Possibly check for img.Source != null first)
BitmapImage source = img.Source as BitmapImage;
if (source != null)
{
//If img.Source is not null the cast worked.
}
try
{
BitmapImage source = (BitmapImage)img.Source;
//If this line is reached it worked.
}
catch (Exception)
{
//Cast failed
}
You could also check the type beforehand using img.SourceisBitmapImage.
How about using WriteableBitmap to make a copy of the image, and then using a MemoryStream to copy the original image into a copy?
// Create a WriteableBitmap from the Image control
WriteableBitmap bmp = new WriteableBitmap(imagetaginxaml, null);
// Load the contents of a MemoryStream from the WritableBitmap
MemoryStream m = new MemoryStream();
bmp.SaveJpeg(m, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
// Read from the stream into a new BitmapImage object
m.Position = 0;
BitmapImage image = new BitmapImage();
image.SetSource(m);
// do something with the new BitmapImage object
// (for example, load another image control)
anotherimagetaginxaml.Source = image;

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

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.

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;

Resources