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.
Related
A single image can be initially loaded from an application subfolder into a Windows form to appear as the BackgroundImage by specifying name and image format by file extension.
However I can't figure out a way to support different image formats with identical names and effectively handle multiple images in my subfolder which have the same name, but different image file extensions.
I want that any supported Bitmap image called map_default(.) will be loaded initially as BackgroundImage if there is a single image with this name in the subfolder and that one image will be picked randomly if there are multiple images in the subfolder like this:
application1.exe
defaultmap(folder):
-map_default.png
-map_default.gif
-map_default.bmp
-map_default.tiff
-map_default.jpeg
I saw many methods to modify path expressions, but unfortunately there seems no way to define the name of an image and specify different file extensions for it in one path expression.
On the other side it seems that I must specify an extension for an image.
I need to know the right path expression and a way to check if there is only one image in the array or several images(in this case pick one item from the array).
How could I possibly solve this?
Well, I don't have an idea for the random picking, but I my problem is always that I don't know how to read the image without specifying an extension when I load it into the Form initially.
There are many working solutions with file selector and drag and drop, but I'm unsure what to do when I load the image automatically when the Form runs:
BackgroundImage = Image.FromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), #"defaultmap\map_default.png"));
Can I use System.IO.Directory.GetFiles path expression with any extension?
Something like this.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//declare path to images directory
string path = #"C:\MyPath\images\";
//fill an array with file names.
string[] images = Directory.GetFiles(path);
if (String.IsNullOrEmpty(images[0]))
{
//set background to hex code color
}
else if (String.IsNullorEmpty(images[1]))
{
//set background to images[0]
}
else
{
//set background to random array value for images
}
}
}
If you just refuse to have a dedicated directory for images you can check for each value in the array ends with .png, .jpg, .bmp, etc.
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.
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.
I've tried finding the solution but to no avail. I believe what i want is string concatenation...
I have a variable "pictureid=face2"
in my resources folder i have a picture called "face2.jpg".
On the form i have a picture box.
This is the code I cant get to work
pictureBox1.Image = Properties.Resources.(pictureid + ".jpg");
Where am i going wrong? The error says there is an identifier expected.
Image expects an Image or a descendant thereof (Bitmap and Metafile objects), which you can use as you coded, if you add the image to your project resources (edit: I should clarify - to do this, go to Project > Properties > Resources tab and "Add Resource". Don't just drop it in the folder):
pictureBox1.Image = Properties.Resources.face2;
If you don't want to include the image in your project, you can use ImageLocation, which will accept a string rather than an object:
pictureBox1.ImageLocation = pictureid + ".jpg"; //assuming you include it in the same folder as the exe
You could also do something like this:
Image face2 = Image.FromFile(pictureid + ".jpg");
pictureBox1.Image = face2;
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.