C# :Emgu CV creating image problem - c#

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

Related

How do I copy an image to the clipboard in Selenium?

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

Set Window background as image from drive on runtime

Images shows in designer but when ran as debug in vs2013 it gives error:
also if exe ran directly with image in same folder.
information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '12' and line position '10'.
The error is bcoz it cant find the image. also in xaml view when hover the image sorce it says:
project file expected in c:\user\bsienn\docs\vs2013\project\wpf1\wpf1\image1.jpg
though the pic is indeed in that path and is available.
I want to add an image as background for form, I don't want to add the image in resource, bcoz i want the image to be changed when needed. I have placed the image with exe file also tried to place it in bin/debug and main application folder (wpf1/image1.jpg andalso wpf1/wpf1/image1.jpg).
here is the xaml code, please guide
<Window.Background>
<ImageBrush ImageSource="image1.jpg"/>
</Window.Background>
App structure:
app.exe
image1.jpg
Desired outcome, form with background image
This will do as desired
XAML:
<Window.Background>
<ImageBrush x:Name="MainFormBgrImg"/>
</Window.Background>
code behind:
BitmapImage bitimg = new BitmapImage();
bitimg.BeginInit();
bitimg.UriSource = new Uri(#""+AppDomain.CurrentDomain.BaseDirectory+"backgroundImg.jpg", UriKind.RelativeOrAbsolute);
bitimg.EndInit();
MainFormBgrImg.ImageSource = bitimg;
AppDomain.CurrentDomain.BaseDirectory:
Returns current working directory from where where app ran from, i.e c:\users\admin\Desktop\
Putting image in output folder won't make it available to your XAML.
You need to add image in your project and set it's Build Action to Resource.
Right click on added image in project -> Open Properties -> Set Build Action to Resource.

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.

Visual Basic 2010, Picturebox resource && Variable?

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;

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.

Categories

Resources