Image inside stackpanel is not getting displayed - wpf

I am creating stackpanel inside the gridview cell dynamically and placing a image control inside stackpanel but on the window only blank space coming, not the image
here is what i am doing
StackPanel Stkpnl = new StackPanel();
Image BgImg = new Image();
BitmapImage Img = new BitmapImage(new Uri(
"Images/cellbg.png", UriKind.Relative));
BgImg.Source = Img ;
Stkpnl.Children.Add(BgImg );

You need to use a Pack URI. See https://stackoverflow.com/a/1651397/486660
If you have a question as to what your Pack URI should look like, then temporarily add an Image control to your WPF page then goto the Source property and click the "..." button and navigate to your image. The property's text box will have the Pack URI in it.

Your code seems to be correct , have you check that image is existing on the folder or the relative path is correct, if you have added this image later to the image folder than you
have to set "Build Action" of a image to "Resource". right click on the image in solution explorer than you can set this property.
you should also check this question
Setting WPF image source in code

StackPanel Stkpnl = new StackPanel();
Image BgImg = new Image();
String packuri = "pack://application:,,,/yourassemblyname;component/cellbg.png";
BgImg.Source = new ImageSourceConverter().ConvertFromString(packuri) as ImageSource;
Stkpnl.Children.Add(BgImg );
PS: To find the assembly name in visual studio 2012 go to "Project" in menu bar -> "setup properties"->Application.

Related

Stop/Start button binding in WP7

There's a button for playing music on the form and a media element. The button is used for play/stop the music.
Is it possible (and how) to draw an arrow or a square inside the button by clicking in XAML? It must depends on the state of the media element (if it's playing or not). If it's impossible how to do it in code behind?
P.S. In other words it the music is playing the button must displays a square, otherwise it's must be an arrow (like a triangle).
You can change the background image of the button through code.
if(mediaplays)
{
ImageSource img = new BitmapImage(new Uri("imageurl", UriKind.Relative));
ImageBrush imgb = new ImageBrush();
imgb.ImageSource = img;
btn.Background = imgb;//btn is the name of the button
}

How to set window icon in code behind in 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);

save writableimage to file in silverlight

How want to save the image of a canvas to a file.
var img = new WriteableBitmap(canvas1, null);
Image i = new Image();
i.Source = img;
var bitmap = new Bitmap(i);
I tried to use bitmap.Save( for saving the image but Bitmap is not supported by silverlight.
How would you save WriteableBitmap to a file?
The WriteableBitmap has a Pixels collection which can be used to access the rendered image. However you really need to get it stored in a known format (preferable PNG).
The imagetools codeplex project can do that for you.
See this blog for a simple example of using it for your purposes.

Displaying loading progress for Image control in WP7

How do I get loading progress with the percent loaded info when an image is loading?
I have this:
Image image = new Image();
image.Source = new BitmapImage(new Uri("http://somesite.com/someimage.jpg"));
I expected something like this:
image.Loading += RoutedEventHandler(image_Loading);
but I can't find any such event. There is Loaded (not related to loading the source) and ImageOpened (which fires after loading the source is complete and has effected a layout pass).
I know it is possible because I have seen other apps indicate loading progress for images (for example "img news reader"). Is this possible with the standard Image Control, is there a 3rd party control that provides this, or do I have to write my own?
DownloadProgress is the event I was looking for, and it was hiding in the BitmapImage class:
Image image = new Image();
BitmapImage myBitmap = new BitmapImage(new Uri("http://somesite.com/someimage.jpg", UriKind.Absolute));
myBitmap.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(myBitmap_DownloadProgress);
image.Source = myBitmap;

transparent png using wpf in VS2008

I want make a UI that is semi transparent in WPF VS2008, so I made my form transparent and I want to show a semi transparent png (Which includes "holes") on top of it.
How do I show the semi transparent png?
Semi transparent, meaning it has holes you can see through.
Also how can I get this done in C#, without using WPF.
Thanks.
You should just have to use the Image control and WPF should take care of the rest:
<Image Source="myimage.png" />
Or in pure C#:
BitmapImage sourceImage = new BitmapImage();
sourceImage.BeginInit();
sourceImage.UriSource = new Uri("myimage.png", UriKind.RelativeOrAbsolute);
sourceImage.EndInit();
Image myImage = new Image();
myImage.Source = sourceImage;

Resources