Setting a picture from my Resources programmatically to a PictureBox - c#

How can I set a picture to a PictureBox in code?
The code below gives me the error:
Cannot implicitly convert Bitmap to
String.
private void ptbLocalidadAdd_MouseEnter(object sender, EventArgs e)
{
ptbLocalidadAdd.ImageLocation = Properties.Resources.addg;
}

If the resource is a bitmap, this should work:
ptbLocalidadAdd.Image = Properties.Resources.addg;

Related

I want to change the background color of panel by choosing the colors from dropdown list

I am writing code which lets user to choose the color of background of page(drop down list) and the problem is that i got the known colors from library System.Drawing and i don't know how to get that values from list. how i can do it?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] colorArray = Enum.GetNames(typeof(KnownColor));
drpBack.DataSource = colorArray;
drpBack.DataBind();}
// page load where i took the names of colors;
//Here is the button update
protected void btnUpdate_Click(object sender, EventArgs e){
string back = drpBack.Text;
Color style = (Color)Enum.Parse(typeof(Color), back);
pnlCard.BackColor = style;}
When i click button update it gives me error : Type provided must be an Enum.
I tried to write instead of Color - KnownColor it gave me message: Can not implicitly convert type "KnownColor" to "Color"
You should parse the input back to KnownColor. Then you can create a Color from this value using Color.FromKnownColor():
protected void btnUpdate_Click(object sender, EventArgs e){
string back = drpBack.Text;
var parsedColor = (KnownColor)Enum.Parse(typeof(KnownColor), back);
var style = Color.FromKnownColor(parsedColor);
pnlCard.BackColor = style;
}

Image.FromFile and picturebox won't display image from computer

I'm using Winforms and have a picturebox. When the user clicks on it, I want to load a image from my computer. But its not working. Any ideas why?
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile("C:/wamp/www/cyber.jpeg");
pictureBox1.Image = new Bitmap(#"C:\wamp\www\cyber.jpeg");
}
If I click on it, I get the error "FileNotFoundException was unhandled". If I remove the Image.FromFile and use the Bitmap instead, I get the error "Parameter is not valid"
try this
pictureBox1.Image = Image.FromFile(#"C:\wamp\www\cyber.jpeg");
Assuming your filepath is correct , I think You have .jpg as extension for your image but you are trying to access with extension .jpeg
Try This:
pictureBox1.Image = Image.FromFile("C:/wamp/www/cyber.jpg");

Previewing images in a listBox using pictureBox

I am using Winforms to create a 2D Map Editor.
I want to be able to preview an image of my assets that are stored in listBox using a pictureBox.
My current code for doing so is thus.
private void listBox_Assets_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(((FileInfo)listBox_Assets.SelectedItem).FullName);
}
But when I select an asset I get this error.
Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.
I have searched high and low for a solution but can't find an answer to this error, any help would be greatly appreciated.
You use the file name from the listbox like this, and protect the code with a check for the file.
private void listBox_Assets_SelectedIndexChanged(object sender, EventArgs e)
{
string file = IO.Path.Combine("the directory", listBox_Assets.SelectedItem);
if (IO.File.Exists(file))
pictureBox1.Image = Image.FromFile(file);
}

Can i check is MouseClick inside certain area?

I have PictureBox in my Windows Form.When I click in some part of the picture, some label text needs to be changed.
Is there any way to know is it clicked in some part of the image?
I didn't gave any code because i think you can understand my problem whitout it.
I suppose you have rect as your Rectangle with some initialization, its coordinates are relative to your PictureBox:
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if(rect.Contains(e.Location)){
//your code here
}
}
You should create mapping for your image, something like this:
Rectangle rect1 = new Rectangle(/*coordinates of part of image*/);
OnClick(object sender, MouseEventArgs e)
{
if (rect1.Contains(e.Location))
{
//handler for this part
}
}
Create List of Rectangle containing areas you want to be interactive.
let say:
private static List<Rectangle> rects;
populate it with desired coordinates in some order.
then in OnClick event
OnClick(object sender, MouseEventArgs e)
{
for(int i=0; i<rects.Count; i++)
if (r.Contains(e.Location))
ActionForArea(i);
}
also
private static void ActionForArea(int number)
{
//do sth
}

C# Forms - Loading images

I'm trying to load a specific image in my picture box if a specific radio button is checked.
The run-time error was a "_Couldn't locate file in this directory.", so I moved the images to that directory, but that just caused the picture box to load it automatically.
I've also imported all of the images I need as well.
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
pictureBox1.Load("10C.jpg");
}
}
When I run the program pictureBox1 had already loaded the image. What I want my pictureBox1 to do is remain blank until the user selects a radio button and clicks the "change image" button.
You should place pictureBox1.Visible = false; in the form load event
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}
You could make it so that pictureBox1.Visible = false;
Then you could say:
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
pictureBox1.Visible = true;
pictureBox1.Load("10C.jpg");
}
}
This way the image is not seen until the button is clicked.
I would recommend you to set a default image in picture box at startup and if a radiobutton is checked and a button is clicked load the desired image (10C.jpg) into picture box instead of dealing with the visible property of the picture box.
The code will look like:
private void button1_Click(object sender, EventArgs e)
{
// pictureBox1.Visible will be always set to true
if (radioButton1.Checked)
{
pictureBox1.Load("10C.jpg");
}
else
{
pictureBox1.Load("placeholder.jpg");
}
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Visible = true;
pictureBox1.Load("placeholder.jpg");
}
Recommended: search google for placeholder image
Hope it helps!
EDIT
In response of:
The run-time error was a "_Couldn't locate file in this directory.",
so I moved the images to that directory, but that just caused the
picture box to load it automatically. I've also imported all of the
images I need as well.
Define a key in config file that holds an image path and use it to access your images by concatenating filename at the end. With this approach you can change image path even after deployment.

Categories