I'm developing WPF with C# and I have this code to get the picture angry.png from the smiles folder in the output folder (e.g. bin\debug):
BitmapImage bitmap = new BitmapImage(
new Uri(#"smiles/angry.png",
UriKind.RelativeOrAbsolute));
But I want to get the picture angry.png from another location like Resources\Image\smiles\angry.png. Could you tell me how to change the folder location so that this would work?
In that case ..\..\Resources\Image\smiles\angry.png should be sufficient.
Related
Basically I want to display the thumbnail of a contact.
So I build a ValueConverter, that should convert the Contact.Thumbnail property (type is IRandomAccessStreamReference) to the source of an Image.
I can load a BitmapImage from an url or a RandomAccessStream, but how do I get from IRandomAccessStreamReference to an RandomAccessStream? Or am I already starting with the wrong approach?
This seems to be the way I was able to get it to work. Get the path to the file and return a BitmapImage. You can bind to the BitmapImage as your image source.
BitmapImage bmp = new BitmapImage(new Uri(((StorageFile)contact.Thumbnail).Path));
Let me know if this helps.
I am trying to add Images to my WPF application Canvas.
From what I understand, they need to be referenced as Resource in the VS Solution.
However, I need to be able to copy an image into a folder, and from an XML file the relative Uri of the image is parsed, and the image is loaded into the canvas :
Image image = new Image();
var pic = new BitmapImage();
pic.BeginInit();
pic.UriSource = new Uri(url, UriKind.Relative); // url is from the xml
pic.EndInit();
image.Source = pic;
LayoutRoot.Children.Add(image); //since the image is not in VS marked as Resource,
// nothing shows up
Thank you for your kind advices
If you specify the full path for the URI instead of using a UriKind.Relative uri, it will work properly:
pic.BeginInit();
pic.UriSource = new Uri(#"C:\Path\To\File.jpg");
pic.EndInit();
I'll be able to program a short program where you can click on a canvas and you'll get the RGB color value for the clicked pixel.
At first i had the graphics on my own HDD with absolute paths.
Problem is, that the program will crashed on each other pc ;)
So, i had created a folder named "Recourcen" within the Project explorer.
Inserted are all the graphics i need.
If i define some images in wpf and write it as follow
...Source="/Resourcen/color_wheel_730.png"
The Image get the right source.
But if i want to use it for a canvas in source, it doesn't run without getting errors.#
canvas_colorpick.Background = new ImageBrush
{
ImageSource = new BitmapImage(new Uri(#"..\..\Resourcen\color_wheel_730.png", UriKind.Relative))
};
I had tested some formattings, but nothing will work in the source code section :(
After some hours of testing i thought i must be put a question to these forum.
Hope somebody can help me with my noob question above.
Greetings from Germany!
Bjoern
Make sure you have set the BuildAction as Resource for added image file in your project.
Also, try using WPF Pack URI to provide the uri source:
canvas_colorpick.Background = new ImageBrush
{
ImageSource = new BitmapImage(new Uri(#"pack://application:,,,/Resourcen/color_wheel_730.png"))
};
Note - Assuming folder Resourcen folder added in same project where XAML resides.
I am trying to set a WPF image control's source to a UNC path image.
My current code goes similar to the following
Button.Content = new Image
{
Source = new BitmapImage(new Uri(UncPathAsString)),
VerticalAlignment = VerticalAlignment.Center
};
However, instead of loading the UNC path of the format:
\\Machine\Folder\Subfolder\Image.jpg
Uri returns the path of:
File:///Machine/Folder/Subfolder/Image.jpg
I have tried creating a bitmap for the UNC path and then converting the Bitmap to a BitmapImage but the performance was too slow for the application. I.E. There are up to several hundred of these buttons being created, and converting the bitmap made load times horrible.
I display an image using c# in a pictureBox. Then I want to change the image's name. I can't because I get that the image is being used by another process.
I open the image this way
Image image1 = Image.FromFile("IMAGE LOCATION"); ;
pictureBox1.Image = image1;
Then try to change it's name this way and I get an IO exception saying " The process cannot access the file because it's being used by another process.
System.IO.File.Copy(#"OldName", #"NewName"); //copy changes name if paths are in the same folder
Isn't the object image1 holding the image? Why is the file still locked by the previous process? Any suggestions would be appreciated. Many thanks!
Have a look at this question: Free file locked by new Bitmap(filePath)
To free the lock you need to make a copy of the bits:
Image img;
using (var bmpTemp = new Bitmap("image_file_path"))
{
img = new Bitmap(bmpTemp);
}
Yes, picture box control locking IO file on the disk in that way.
If you want to rename linked file, you can:
or dispose picture box, and after rename the file (if this is suitable for your app)
or assign to the picture box a copy/clone of the image.
You can try this:
Image image1 = null;
using(FileStream stream = new FileStream("IMAGE LOCATION", FileMode.Open))
{
image1 = Image.FromStream(stream);
}
pictureBox1.Image = image1
;
you need to dispose your image:
Image image1 = Image.FromFile("IMAGE LOCATION"); ;
pictureBox1.Image = image1;
image1.Dispose();
the do the move.
this solution solved my problem. I was using a picturebox and loading its image directly from the disk without an intermediate image file. when I was done I was doing a "picturebox.dispose" and that did not free up the image file and I couldn't delete it because "file in use by vshost". I had to explicitly create an image variable (IE dim MyImage as image) and then first load the image into the image variable, then load MyImage into the picturebox. by having a reference to the image I could do a MyImage.dispose. That closed the link between vshost and the image and I was able to programmatically delete it.