Image Source wont show picture - c#

I have problem with setting Image Source property... So I tried use every variation of this code and for some reason it won't work.
When I set property manually it works, but when I want to change picture in code it is just blank.
`BitmapImage` bm = new BitmapImage();
bm.UriSource=new Uri(this.BaseUri,#"\\Assets\logo6.png");
this.image.Source = image;
I use this code inside of button event so I can change picture inside image control.

you are setting the path in wrong way. try any of these two options it will work.
Also make sure your image property Build Action is should set to Content and Copy To OutPut Directory to Copy to newer or Copy Always
BitmapImage bm = new BitmapImage();
bm.UriSource = new Uri(#"\Assets\Tiles\IconicTileSmall.png",UriKind.Relative);
image.Source = bm;
And
BitmapImage bm = new BitmapImage();
bm.UriSource = new Uri("\\Assets\\Tiles\\IconicTileSmall.png",UriKind.Relative);
image.Source = bm;
Basically you are mixing these two ways.

Related

How to convert string to bitmap image

please tell me how to convert text or string in bitmap image in wpf in c#.because there is no bitmap class is available like windows form..
Thanks for Advance :)
BitmapImage is what you are looking for. Refer to the sample provided in MSDN link.
// Create the image element.
Image simpleImage = new Image();
simpleImage.Width = 200;
simpleImage.Margin = new Thickness(5);
// Create source.
BitmapImage bi = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.BeginInit();
bi.UriSource = new Uri(#"/sampleImages/cherries_larger.jpg",UriKind.RelativeOrAbsolute);
bi.EndInit();
// Set the image source.
simpleImage.Source = bi;
Also, you can set the source directly in XAML which WPF internally convert to ImageSource via default converter for String to ImageSource.
<Image Source="/sampleImages/cherries_larger.jpg"/>

Buttons not aligned correctly in WPF

The customized controls have two images, a background and a foreground image. I use the following code to display the controls. This function is called twice, once for background image displaying and then for the foreground image displaying
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(imgpath, UriKind.Absolute);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
ImageBrush ib = new ImageBrush(src);
ib.Viewbox = new Rect(UVRectangle.X / src.PixelWidth, UVRectangle.Y / src.PixelHeight, UVRectangle.Width / src.PixelWidth, UVRectangle.Height / src.PixelHeight);
Image image = new Image();
image.Source = ib.ImageSource;
gr.Children.Add(image); //gr is of type Grid
The buttons are displayed perfectly but I realized that their alignment is incorrect. When I place one button right below another button, the one below appears to be shifted to the right.
This problem of alignment is rectified by changing
Image image = new Image();
image.Source = ib.ImageSource;
gr.Children.Add(image);
to
gr.Background = ib;
But, this makes the foreground image completely cover the background image like I had asked here
What should I do to prevent this alignment problem? Also, what is causing this problem in the first place?
Did you try putting the buttons inside stackpanel? http://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel.aspx

WPF Rectangle FIll imagebrush not updating

I am developing a WPF application, a messenger client. The user should be able to change his avatar image. When he right-clicks his avatar, a open file dialog appears and there he can select the image he wants. After he has made his decision I delete his current avatar and copy the new one in the same place with the same name. The avatar image is a rectangle with an imagebrush fill. The problem is that the image does not update until I restart the application. Here is the piece of code used to load the new image.
if (x.ShowDialog() == true)
{
ImageBrush img = new ImageBrush();
BitmapImage bit = new BitmapImage();
Bitmap bmp = new Bitmap(x.FileName);
System.IO.File.Delete(System.IO.Directory.GetCurrentDirectory() + "/data/images/avatar/x.png");
bmp.Save(System.IO.Directory.GetCurrentDirectory() + "/data/images/avatar/x.png", System.Drawing.Imaging.ImageFormat.Png);
bit.BeginInit();
bit.CacheOption = BitmapCacheOption.OnLoad;
bit.UriSource = new Uri(#"pack://siteoforigin:,,,/data/images/avatar/x.png");
bit.EndInit();
img.ImageSource = bit;
ProfileImage.Fill = img;
}
I hope you can help me solve this issue or offer me some alternatives. Thank you all!
In order to make BitmapImage reload the image file (with identical file path), you could set the BitmapCreateOptions.IgnoreImageCache option:
bit.BeginInit();
bit.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bit.CacheOption = BitmapCacheOption.OnLoad;
bit.UriSource = new Uri(#"pack://siteoforigin:,,,/data/images/avatar/x.png");
bit.EndInit();
img.ImageSource = bit;

how to get the image path on image_tab in windows phone 7

I have tried this but I am not getting the answer. I have 3 image box. In this 2 image box have images and 1 image box is empty. Now I want to change the image on image_tab event, can any one help me to find this?
Uri myfile = new Uri("Images/star.png", UriKind.Relative);
StreamResourceInfo resourceInfo = Application.GetResourceStream(myfile);
BitmapImage myimage = new BitmapImage(myfile);
myimage.SetSource(resourceInfo.Stream);
image1.Source = myimage;
I am not sure what exactly image_tab is in your case (maybe Image_Tap?), but you can simplify your image source assignment by using:
Uri uri = new Uri("PATH", UriKind.Relative);
BitmapImage imageSource = new BitmapImage(uri);
image.Source = imageSource;
Remember, that the ImageSource object does not expose the path directly, so you might want to have an internal reference location where paths to files you use are handled.

Get picture data from Image control WPF

I'm new to WPF. I was programming for Windows Forms Applications, and there was Picture Box with Image property. But in WPF there is Image instead of Picture Box. How to get it's value? I mean the image data.
you can do that by using Image.Source property.
You will find a lot of samples on internet about it. For example code below sets image source, you can get the image similarly
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;
//get the source
BitmapImage imgSource = myImage3.Source;

Categories