How to set window icon in code behind in wpf? - wpf

In xaml it is :
<View:BaseWindow.Icon>
/VBDAdvertisement;component/Images/logoVBD.png
</View:BaseWindow.Icon>
I want to convert it into code behind.
Thanks

Something like
myWindow.Icon = new BitmapImage(new Uri("/VBDAdvertisement;component/Images/logoVBD.png"));
You may need to qualify the path more though.
Edit: As i thought the path should be in pack-uri format:
"pack://application:,,,/VBDAdvertisement;component/Images/logoVBD.png"

This is the correct way to do it (assuming MyIcon.ico is placed on the root folder of a WPF project named MyApplication):
Uri iconUri = new Uri("pack://application:,,,/MyApplication;component/MyIcon.ico");
myWindow.Icon = BitmapFrame.Create(iconUri);
This is also what actually happens when you set the Icon property for the window in XAML.
When just setting the Icon to a new Bitmap, it will not be rendered smoothly and correctly, but instead quite a bit pixelated.

Try this its absolutely working for both png as well as ico image format.
window.Icon = BitmapFrame.Create(Application.GetResourceStream(new Uri("LiveJewel.png", UriKind.RelativeOrAbsolute)).Stream);

Related

Is there any way to assign multiple images source at one time

Is there any way in silverlight to assign multiple images at time to selecting single image and set it's source it will assign to all the images in the wrappanel.
Thanks...!!
Maybe something like this?
foreach(Image image in imageWrappanel.Children) {
image.Source = selectedImage.source;
}
Assuming you have a wrappanel named imageWrappanel and the selected Image is called selectedImage.

WPF Image Color Tint

I am loading several images in WPF and from what I have read, it keeps them all in memory.
I was wondering why it is that when I display my image, that there is a red/purple tint?
Has anyone experienced this issue before?
I set up an Image class using the Designer, then I set the Image.Source to my ImageSourceConverter.ConvertFromString("MyFilepath.png")
I also set
Image.Stretch = Stretch.Fill (if that helps)
It seems that some images have a reddish discoloration to them. I cannot figure out why...?
EDIT:
I tried to post images, but stackoverflow's convenient spam prevention said I could not post them because I just joined...
Is it possible there's a translucent overlay in your XAML causing the discoloration? It could also be an encoding issue... Can you post a sample of your code?
Also, although you can't embed images directly in your post because of your newness, you could upload the image to imgur.com, for example, and post a link to that image.
Well, I found that after I changed the images from .png to .jpg, the discoloration was no longer an issue. I am wondering what might have caused this?
I do not think it was the alpha transparency of the .png's, but rather the ImageSourceConverter itself...?
This was most likely caused by dithering effect. It should go away after you set the following property on your Image element to true:
SnapsToDevicePixels = "True"

How to change image source on runtime?

I get new bitmap from some other component ( dont have any control on the other component ) every 5 seconds and i need to update my wpf image control with the new bitmap ( every 5 seconds ... ).
I cant find any way to update this wpf image control in run-time.
How can i do it ?
Thanks.
There is a another question like this
Setting WPF image source in code
the answer boils down to
ImgOnForm.Source = new BitmapImage(new Uri(#"/yourApp;component/img.png", UriKind.Relative));
Here is what worked for me :
chemin = "/Assets/Images/" + nomFichier + ".gif";
MonImage.Source = new BitmapImage(new Uri(base.BaseUri, chemin));
None of the suggestions I found on the forums worked (I had no image, or an ArgumentException). But with "base.BaseUri" as first agument, it worked at last.

Make part of an image transparent

I want to put an image on a button, but I want part of the image to be transparent. How do I do this?
Try the Image.OpacityMask property. You can give it a brush that specifies the region you want to be transparent.
EDIT: From MSDN:
There is no direct support for
color-keying a bitmap in WPF.
However, it is fairly easy to
implement on your own. Dwayne has
implemented a ColorKeyBitmap on his
blog:
http://blogs.msdn.com/dwayneneed/archive/2008/06/20/implementing-a-custom-bitmapsource.aspx
I believe it links to the code on
Codeplex as well. You could also
accomplish this simply by reading your
bitmap into system memory, iterating
through all the pixels and setting
their values yourself, and
constructing a new bitmap out of that
array.
Use a paint program (I use Paint.Net) to change the area you want transparent to an alha=0 color. Then save the image (mine was JPG) as a PNG. Seemed to work fine for me in the WPF Image control.

How Can I Get Information About the Source Image of a WPF `<Image>` Element?

How can I get information about the source image of a WPF <Image> element?
My <Image> element has its Source bound to an ImageSource property, which changes frequently. In my code behind, I need to be able to access the actual width of the current source image file (in regular pixels), for mathematical purposes. My application will perform image operations on the image file, so this information is necessary.
Any help is appreciated.
I think this may work for you:
BitmapSource sourceData = image.Source as BitmapSource;
int width = sourceData.PixelWidth;
int height = sourceData.PixelHeight;

Resources