Properties.Resources not loading an image - c#

I have a game application in Visual Studio 2012 C#. I have all the .png images I am using in the Resources file of the project.
Have you any idea why I can access all the files but one by using Properties.Resources?
I checked the full filePath and it's set to the resources folder. And it's added in the program as I did Add -> Existing Item and added the image.
It looks just like the other images. I have no idea why it's not loading. I need this since I need to send a .exe by email to my lecturer and without this image the project is nothing!
I added this in the resource file
internal static System.Drawing.Bitmap grid_fw {
get
{
object obj = ResourceManager.GetObject("grid.fw", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
and although now grid is available, it is returning null :/

Found from: Properties.Resources the icon name does not appear in the intellisense
You also need to add the icon to the Resources.resx file. Open it in
Visual Studio and drag your icon into the Icons menu of the resx and
it will become available.
Also, see Adding and Editing Resources (Visual C#)

You can get a reference to the image the following way:
Image myImage = Resources.yourImage;
If you want to make a copy of the image, you'll need to do the following:
Bitmap bmp = new Bitmap(Resources.yourImage);
Don't forget to dispose of bmp when you're done with it. If you don't know the name of the resource image at compile-time, you can use a resource manager:
ResourceManager rm = Resources.ResourceManager;
Bitmap yourImage = (Bitmap)rm.GetObject("yourImage");
The benefit of the ResourceManager is that you can use it where Resources.myImage would normally be out of scope, or where you want to dynamically access resources. Additionally, this works for sounds, config files, etc.

Related

Overwriting and Image Open and Currently Binded to In WPF Program

I am binding to 5 different images in my program and wish to leave the capability of the user to replace or update the photos under the same name. So when updating these pictures, the binding will be notified and change while the program is running.
The program is a Digital VMB (visual management board) at my workplace, so it needs to remain running and have these photos be updated on the server without a hitch. Currently I am binding to a property in my C# which is a string containing the image location.
In c#:
public string OpenFilePathProp { get; set; }
In XAML:
<Image x:Name="OpenWOImage" Source="{Binding OpenFilePathProp}" Stretch="Fill" Margin="25,0,25,0"/>
When the user goes to copy and replace the image with the newer one, they can't as it's "currently open in another process". Which I suppose is the data binding in my WPF.
Can this be overcome by opening the images into a filestream and then binding to the stream? If so, I'm completely unsure on how to bind to a filestream; I'm quite new to WPF AND C#.
Thanks for the help. I HAVE tried to look for a solution to this, but I think I'm just getting confused and I don't think it will resolve my problem since it seems the binding is what's "keeping the image open" and I'm not sure how to bind to an Image object AND close that object to allow for overwriting
EDIT: Thought I should mention that I've managed to copy the images in question to the AppData folder for my VMB program,
like,
string AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\";
System.IO.File.Copy(Properties.Settings.Default.OpenWOFilePath, System.IO.Path.Combine(AppData, "ROAMaintenanceVMB\\OpenWOFilePath_Copy.jpeg"), true);
this way I can "check" every so often to see that someone has overwritten the photos on the server, and THEORETICALLY copy the "new" photos to the AppData folder, overwriting the previous versions. THIS is where the issue of the images already being open in another program arises.
Following Peter Duniho suggestion about using WriteableBitmap worked in my instance.
string imagePath = "";
Uri imageUri = new Uri(imagePath);
BitmapImage bitmapImage = new BitmapImage(imageUri);
ImageProperty = new WriteableBitmap(bitmapImage);
<Image Source="{Binding ImageProperty}"/>
What's going on here is:
BitmapImage is created from the given path
WriteableBitmap is created based on the BitmapImage (a "deep copy"?)
The important thing here is the original image is not locked, as the binding is tied to the copy of the image of WriteableBitmap - not to the image itself, so the image can be deleted / replaced freely.

C# WPF ImageControl not showing selected image [duplicate]

This question already has answers here:
WPF - Import image as resource
(3 answers)
Closed 7 years ago.
I'm using VS2013. .NetFramework 3 . I'm writing a simple project like as image gallery.
I'm trying to add an JPEG image into Image Control. First I'm adding the test.jpg image as resource. Then I'm adding an Image Control on to Window. In the next step I'm selecting the image in "Source" property. The image displaying into design mode. All is ok. But when I'm running the project nothing displaying.
I searched in google and youtube. I founded some solutions but I haven't solution for my problem
(Sorry for ENG)
Here the code line
<Image Source="pack://siteoforigin:,,,/Resources/1.JPG" ... />
In order to simply display the image in WPF app, do the following:
1). In XAML, add:
<Image Name="imgName" Source="/ProjAssemblyName;component/RelativePathToImageFile" />
2). In Visual Studio IDE, select the image file and check its properties:
Build Action: Resources
Copy to Output Directory: Do Not Copy
Note: in most typical case the project Assembly name is the same as Default Namespace; check it in application property dialog. Also useful info pertinent to your case is included in the Microsoft reference (as pointed out by member #Clemens): https://msdn.microsoft.com/en-us/library/aa970069%28v=vs.110%29.aspx#Resource_File_Pack_URIs___Local_Assembly.).
Another option is to set the image Build Action property to EmbeddedResource and get the BitmapImage object corresponding to the image file programmatically like the following:
BitmapImage _bmpImage = new BitmapImage();
_bmpImage.BeginInit();
_bmpImage.StreamSource = Assembly.GetExecutingAssembly().GetManifestResourceStream(String.Concat(ProjAssemblyName, ImgFile));
_bmpImage.EndInit();
This technique is useful if further image processing is considered.
Hope this may help. Kind regards,

Loading PictureBox Image from resource file with path (Part 3)

I understand that this question has been asked (and answered) before. However, none of the solutions are working for me.
Below is a screen capture of all the relevant pieces of the puzzle:
Screen capture http://dinosaur-island.com/PlantPictureBoxScreenCap.jpg
As you can see there are numerous bitmaps of plants loaded as resources into the Images folder. There is a form with a picturebox named "PlantPicture". There is string, which I know has a good path (because I've checked it in the debugger):
PicPath = PicPath+".bmp";
Screen capture http://dinosaur-island.com/PlantDebugger.jpg
I've tried numerous ways of loading, casting, etc., etc.
The path should be something like: "Images\a.bmp". (Note the lack of a leading slash, and the slashes being back slashes.)
And then:
pictureBox1.Image = Image.FromFile(#"Images\a.bmp");
I just tried it to make sure, and it works. This is besides the other answer that you got - to "copy always".
Ok...so first you need to import the image into your project.
1) Select the PictureBox in the Form Design View
2) Open PictureBox Tasks
(it's the little arrow printed to right on the edge of the PictureBox)
3) Click on "Choose image..."
4) Select the second option "Project resource file:"
(this option will create a folder called "Resources" which you can access with Properties.Resources)
5) Click on "Import..." and select your image from your computer
(now a copy of the image will be saved in "Resources" folder created at step 4)
6) Click on "OK"
Now the image is in your project and you can use it with the Properties command. Just type this code when you want to change the picture in the PictureBox:
pictureBox1.Image = Properties.Resources.MyImage;
Note:
MyImage represent the name of the image...
After typing "Properties.Resources.", all imported image files are displayed...
It depends on your file path. For me, the current directory was [project]\bin\Debug, so I had to move to the parent folder twice.
Image image = Image.FromFile(#"..\..\Pictures\"+text+".png");
this.pictureBox1.Image = image;
To find your current directory, you can make a dummy label called label2 and write this:
this.label2.Text = System.IO.Directory.GetCurrentDirectory();
The accepted answer has major drawback!
If you loaded your image that way your PictureBox will lock the image,so if you try to do any future operations on that image,you will get error message image used in another application!
This article show solution in VB
and This is C# implementation
FileStream fs = new System.IO.FileStream(#"Images\a.bmp", FileMode.Open, FileAccess.Read);
pictureBox1.Image = Image.FromStream(fs);
fs.Close();
Setting "Copy to Output Directory" to "Copy always" or "Copy if newer" may help for you.
Your PicPath is a relative path that is converted into an absolute path at some time while loading the image.
Most probably you will see that there are no images on the specified location if you use Path.GetFullPath(PicPath) in Debug.

FileNotFoundException why trying to open an image

I have an image in one of my project folders:
Lets say its in:
~/App_Themes/Default/images/SomeImage.png
I want to load this image into a System.Drawing.Image, how do I do that?
If I try using the FromFile method of the Image class:
Image img = Image.FromFile("~/App_Themes/Default/images/SomeImage.png", true);
I get a FileNotFoundException.
I have read some suggesting to store the image into the Server but that's not an option. Is there any way to load this into the Image?
You seem to be using a relative path instead of a file path to locate the image. Try this:
var path = #"~/App_Themes/Default/images/SomeImage.png";
using (Image img = Image.FromFile(Server.MapPath(path)))
{
do some stuff
}
I had a similar problem. The problem for me was that I accidentally added Image folder inside of App_Code folder. I did not updated the code accordingly and therefore I was getting exception.
As soon I removed the Image folder out of App_Code folder, the problem was resolved.
Of course I could have updated also the path in the code.

C#: Changing picture in form, using picture from resourses

I have 7 pictures in resources, named 1.jpg, 2.jpg ect..
Then in code I have generated int skaicius, which has value 5...
Now I need on button push avent change the image to skaicius + ".jpg" and use image from imported resources
It's something like that I think
pictureBox2.ImageLocation = kauliukas + ".jpg";
But It's supposed to load image from local dir as I know... SO how to load it from resources?
As soon as you import your pictures to the Resources (resx) file, the extensions are lost, and for example "ThisPicture.jpg" would be accessed through Properties.Resources.ThisPicture.
Which effectively means you can do the following on your PictureBox:
pictureBox.Image = Properties.Resources.PictureName
Where PictureName is the name of the actual resources according to your Resources file.

Categories

Resources