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.
Related
I am writing a test and the functionality I need to replicate is essentially saving a image to the clipboard and paste it later on. I am using Selenium WebDriver v3.11.1.
I have attempted using ContextClick to copy an image in many various ways and it never quite did what I wanted for example:
Actions rightClickAction = new Actions(driver);
rightClickAction.MoveToElement(logo).ContextClick(logo).SendKeys(Keys.ArrowDown).SendKeys(Keys.ArrowDown).SendKeys(Keys.ArrowDown).SendKeys(Keys.Enter).Build().Perform();
But the arrow down/enter never worked because it didn't focus on the right click menu. So then I found this bug https://bugs.chromium.org/p/chromedriver/issues/detail?id=1003 which makes me think that I can't use context click to copy an image. I also, couldn't just 'ctrl+c' the image.
I then learned that I could Clipboard which I couldn't get to set an image from my directory:
Clipboard.SetImage(Image.FromFile("C://Image.png"));
I then tried taking a screenshot as done here: C# Selenium - How do you take a screenshot in Visual Studio 2015 and that didn't work with either. Trying to save the screenshot file and add it to the 'clipboard' got messy.
I have also tried grabbing an image from a page by getting a base64 string of the image with JavaScript that is executed by webdriver, then saving the base64 string of the image to a file, which I found here: Using selenium to save images from page
This also got messy and I wasn't sure how to then save it to the clipboard.
So, how can I save an image to my clipboard?
You can try something similar to this:
driver.get("https://stackoverflow.com/");
WebElement element = driver.findElement(By.xpath("//span[(text()='Stack Overflow') and #class='-img _glyph']"));
WrapsDriver wrapsDriver = (WrapsDriver) element;
File screenshot = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
Rectangle rectangle = new Rectangle(element.getSize().width, element.getSize().height, element.getSize().height, element.getSize().width);
Point location = element.getLocation();
BufferedImage bufferedImage = ImageIO.read(screenshot);
BufferedImage destImage = bufferedImage.getSubimage(location.x, location.y, rectangle.width, rectangle.height);
ImageIO.write(destImage, "png", screenshot);
File file = new File("C:\\tmp\\123.png");
FileUtils.copyFile(screenshot, file);
Let me know if it works for you
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.
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.
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.
When I am trying to create image like
Image<Gray, Byte> testImage = new Image<Gray, Byte>("david.jpg");
When compiling it gaves An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dllexception.
But if I use
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK || result == DialogResult.Yes)
{
textBox1.Text = openFileDialog1.FileName;
}
Image<Gray, Byte> testImage = new Image<Gray, Byte>( textBox1.Text);
It works.Problem is that it cant find path? I am adding all .jpg files in project folder.
This is most likely because the image ("david.jpg") can't be found. There are two ways around this if you will always be using the image.
1/ Use the full file path ("C:\Main_Directory\Sub_Directory\David.jpg")
2/ If you wish to use just ("david.jpg")
Right Click on your project in the 'Solution Explorer' panel, (as you would add a new form or reference) select Add>Existing Item
Browse and locate your Image (Note:You may have to change what file type your looking for in the drop down box below object name. Once selected click Add.
In your 'Solution Explorer' panel you should have the image now in your project. Now the IMPORTANT step as you did with the OpenCV librarys (cv210.dll, cxcore210.dll etc) You must select the image and in the 'Properties' panel change the 'Copy to Output Directory' to either "copy if newer" or "copy always".
This should solve your problem if you are always going to use the image I would suggest option 2 as when you export the program to another user the image will be copied to the bin\deploy directory
Hope This helps
Chris