Assign image to Image.Source - wpf

How to assign a picture Image.Source: Image.Source = "Images \ 1.bmp" - does not work (you can not assign a picture)

Image myImage3 = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("smiley_stackpanel.PNG", UriKind.Relative);
bi3.EndInit();
myImage3.Stretch = Stretch.Fill;
myImage3.Source = bi3;
As mentioned above easily found here

Refer to Pack URIs in WPF
Don't forget to set the Build Action for '1.bmp' to Resource

The Image.Source page at MSDN http://msdn.microsoft.com/en-us/library/system.windows.controls.image.source.aspx has an example. You need to assign an ImageSource, not a string. (but of course see Pack URIs in above post)

Related

BitmapImage from file PixelFormat is always bgr32

I am loading an image from file with this code:
BitmapImage BitmapImg = null;
BitmapImg = new BitmapImage();
BitmapImg.BeginInit();
BitmapImg.UriSource = new Uri(imagePath);
BitmapImg.CacheOption = BitmapCacheOption.OnLoad;
BitmapImg.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
BitmapImg.EndInit();
It works as expected except for the fact that no matter what kind of image I'm loading (24bit RGB, 8bit grey, 12bit grey,...), after .EndInit() the BitmapImage always has as Format bgr32. I know there have been discussions on the net, but I have not found any solution for this problem.
Does anyone of you know if it has been solved yet?
Thanks,
tabina
From the Remarks section in BitmapCreateOptions:
If PreservePixelFormat is not selected, the PixelFormat of the image
is chosen by the system depending on what the system determines will
yield the best performance. Enabling this option preserves the file
format but may result in lesser performance.
Therefore you also need to set the PreservePixelFormat flag:
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(imagePath);
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache
| BitmapCreateOptions.PreservePixelFormat;
bitmap.EndInit();
For some reason I don't understand, using BitmapCreateOptions.PreservePixelFormat with new BitmapImage() didn't work for me. But this did lead me onto the right track. If anyone else tries the other answer and it doesn't work, this alternate method is what worked for me:
var decoder = new PngBitmapDecoder(new Uri(imagePath), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapFrame frame = decoder.Frames[0];
For whatever reason PngBitmapDecoder worked where BitmapImage didn't.
Note: BitmapFrame inherits BitmapSource just like BitmapImage, for my purpose that was all I needed.

Change System.Windows.Controls.Image to MemoryStream?

I have a problem in silverlight which I need to convert this image(from database probably will be in bytes) and convert it to MemoryStream. I need to do this so I can use it to export images to PDF files. Any Ideas?
Image myImage = new Image();
myImage.Source = new BitmapImage(new Uri("Resources/cancel.jpg", UriKind.RelativeOrAbsolute));
For starters, I am trying to convert an Image object to a memory stream object in a sample project(that's why UriSource is coded). Because I'm not familiar with creating dummy data in bytes. Any help on this one? Can I convert an Image to MemoryStream? if not, I believe bytes can be converted to stream, how can I do dummy data?
Thanks for all the replies.
You just need to pass byte[] to MemoryStream constructor
byte[] bytes = GetBytes();
MemoryStream ms = new MemoryStream(bytes);
Hope this helps

Set System.Windows.Controls.Image from StreamReader?

I've got an image in PNG format in a StreamReader object. I want to display it on my WPF form. What's the easiest way to do that?
I've put an Image control on the form, but I don't know how to set it.
The Image.Source property requires that you supply a BitmapSource instance. To create this from a PNG you will need to decode it. See the related question here:
WPF BitmapSource ImageSource
BitmapSource source = null;
PngBitmapDecoder decoder;
using (var stream = new FileStream(#"C:\Temp\logo.png", FileMode.Open, FileAccess.Read, FileShare.Read))
{
decoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
if (decoder.Frames != null && decoder.Frames.Count > 0)
source = decoder.Frames[0];
}
return source;
This seems to work:
image1.Source = BitmapFrame.Create(myStreamReader.BaseStream);
Instead of using a StreamReader, I would directly generate the Stream,
FileStream strm = new FileStream("myImage.png", FileMode.Open);
PngBitmapDecoder decoder = new PngBitmapDecoder(strm,
BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
myImage.Source = decoder.Frames[0];
where myImage is the name of your Image in XAML
<Image x:Name="myImage"/>
Update: If you have to use the StreamReader, you get the Stream by using .BaseStream.

how can I set the metadata of an image using WPF?

WPF has an ImageSource which has a BitmapMetadata.
However, the Metadata property is read only an the BitmapMetadata is frozen.
So I can I modify the metadata of an image?
Unfortunately the WPF imaging API doesn't allow you to modify the image metadata... However, you can still do it with GDI+ (System.Drawing)
Found the answer:
var frame = ...;
BitmapMetadata newMetadata = ...;
var newFrame = BitmapFrame.Create(frame, frame.Thumbnail, newMetadata, frame.ColorContexts);
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(newFrame);

Showing Bitmap Images in WPF via C#

What I am trying to do is so simple but I am having a hard time making it work. I saw some posts along the same lines but I still have questions.
I have a MenuItem object called mnuA. All I want is set the icon property programatically in C#. I have tried the following
a) mnuA.Icon = new BitmapImage{UriSource = new Uri(#"c:\icons\A.png")};
Results: Instead of showing the actual icon, I get the class name (System.Windows.Media.Imaging.BitmapImage)
b) mnuA.Icon = new BitmapImage(new Uri(#"c:\icons\A.png"));
Results: Instead of showing the actual icon, I get the path of the image (file:///c:/icons/A.png)
What am I doing wrong? Do I really need a converter class for something simple like this?
Try this:
Image img = new Image();
img.Source = new BitmapImage(new Uri(#"c:\icons\A.png"));
mnuA.Icon = img;
Might be a long shot, but try something like:
Uri u = new Uri(...); mnuA.Icon = new
BitmapImage(u);
What it seems its happening is that your icon is getting converted to a string.

Resources