using image in if statement - c#

how can i use image in if statement?
for example i want to check in click event of a picturebox that if a specific image(for example image1) is in it,then do some work.thanks in advance
update:
like this
the picturebox can be null or have an image in it,i want to check image1 is in it or not
private void pictureBox34_Click(object sender, EventArgs e)
{
if (///picturebox34=image1)
{
f();
}
}
now when i compile this code i took error:cannot convert system.drawing.image to bool

If you have all the images loaded into memory then you could simply perform a reference comparison between the Image object assigned to the PictureBox and the other Image objects to determine which one is in the PictureBox.
Alternatively when you assign the Image to the PictureBox you can set the Tag property of the PictureBox to identify the Image and use the value of the Tag property for the test in your event handler. Of course in this case you only need the one Image in memory, but you will have to have some kind of identifying information, like the name of the image to assign to the Tag property.
Update: Based on your updated question, it seems you are happy to perform a refernce comparison. Which you can do as follows
private void pictureBox34_Click(object sender, EventArgs e)
{
if (picturebox34.Image == image1)
{
f();
}
}

Related

How to calculate which item of panel was selected

I'm programing a little C# program for browsing pictures, but I got stuck.
I have a Panel full of PictureBoxes, is there any way to calculate number of the PictureBoxes that were clicked on? For example, if I have a ListBox I can easily type listBox1.SelectedIndex and get the number.
I assume that it's not that easy with Panel, but is there actually any way to do this?
It depends on what you actually want to do. There is no built-in property of PictureBoxes that make or mark them selected.
You can get the index of the PictureBox in the Panel's Controls collection in its Click event like this:
private void pictureBox_Click(object sender, EventArgs e)
{
int index = yourPanel.Controls.IndexOf(sender as PictureBox );
}
You can and probably should assign the same event to all PBs' Clicks!
If you simply want to work with the PictureBox write
PictureBox pb = sender as PictureBox;
pb.Image = ...
Or you could loop over the Controls to find one with Focus. But even if it was there that would only be the keyboard focus, and, as there can only be one, this would not persist even a single Button click.. So this is not recommended.
So if you want to refer to the last clicked PB simply store it in a class variable or maybe in the Panel's Tag:
PictureBox selectedPB = null;
private void pictureBox_Click(object sender, EventArgs e)
{
selectedPB = sender as PictureBox;
// or
yourPanel.Tag = sender as PictureBox;
}
If you want to collect several PBs you can do so by adding them to a List:
List<PictureBox> clickedBoxes = new List<PictureBox>();
private void pictureBox_Click(object sender, EventArgs e)
{
PictureBox pb = sender as PictureBox;
if (!clickedBoxes.Contains(pb) ) clickedBoxes.Add(pb);
}
and access the number as the clickedBoxes.Count ..
Try using the 'SelectedItem' property of your ListBox and bind it to a property on the ViewModel.
If you are using just something like StackPanel? Then no, StackPanel does not have this functionality built in. Use a ListBox with a custom Template.

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);
}

Changing a Windows Phone Application's WritableBitmap Object

All, I have a basic Windows 7 Phone application and I have just finished a crop page, where the users are able to crop an image taken with the phones camera. In the cameraCapTask_Completed event I set the App's global WritableBitmap
public static WriteableBitmap capturedImage;
as follows
void cameraCapTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK && e.ChosenPhoto != null)
{
// Take JPEG stream and decode into a WriteableBitmap object.
App.capturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
When I take a picture I then pass it to the cropping page in the CropProcessPage constructor I set the Image in the page via
public CropProcessPage()
{
InitializeComponent();
// Set the text and display captured image.
imageMain.Source = App.capturedImage;
This works. However, when I go back to the Main Page and retake/take another image, when I try to the new image, the old image (the first one taken) is shown. The constructor is being called and so is the camera captured event (setting the new image). What am I doing wrong here?
In CropProcessPage
move line
imageMain.Source = App.capturedImage;
to
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
// Set the text and display captured image.
imageMain.Source = App.capturedImage;
}

Which pictureBox was selected? C#

I am working on a piano in C#. I have encountered a small problem.
I have a piano keyboard which, when pressed, displays the relevant note on the staff.
The notes created are stored in an array of type PictureBox, called picBox. I have constructed the following event handler, however it is not working.
private void pictureBox_Click(object sender, MouseEventArgs e)
{
picBox[0].MouseDown += new MouseEventHandler(pic_Click); //testing for first location
}
private void pic_Click(object sender, MouseEventArgs e)
{
ClickedTextBox.Text = "I was clicked";
}
I am just testing to see if the first note was clicked. Why is this not working?
Here is the method which adds the picturebox (containing the note) to the staff (panel3).
public void addPictureBox(int x, int y, Image image)
{
picBox[cnt] = new PictureBox();
picBox[cnt].Image = image;
picBox[cnt].Location = new Point(x, y);
picBox[cnt].BackColor = Color.Transparent;
panel3.Controls.Add(picBox[cnt]);
picBox[cnt].BringToFront();
cnt++;
}
What is wrong with my event handler please? Also, what can I do to identify the location in the array of the picturebox clicked? Thank you
As said in the first comment, you subscribe to the event in a wrong location.
Also use the sender parameter of your event handler to know which picturebox is clicked (it'll contain an instance of the picturebox).

How do I associate a button with a control?

OR - How To Shave A Koala To Stop It Looking Squashed. (But I didn't think that would make a suitably techy title)
The Problem: You have three preview images derived from a main image. The preview images are resized for standardised picture spaces on a company website, the main image can be any size image from anywhere.
Example: The main image is a hi-res image of a koala bear measuring 2000x2250. Your previews want to render the koala at 200x200, 200x50 and 250x150.
Your utility program resizes and stretches the original image to the size of your three "actual size" previews but obviously each preview looks a bit squashy and you know everyone hates to see a squashed koala.
To resolve this you add a little cropping method to your program which shaves five pixels from the preview on the desired side. This means you should be able to resize your image and unsquash your koala by shaving off the unnecessary parts of the image.
You add four buttons to each preview image picture box and create four generic methods for sending the correct shaving instructions to the crop method. You want to associate each specific button with a specific picturebox on the form, but you want to send all the click events to four generic functions.
How do you tell the generic function which of the three preview picturebox images you want it to shave in an elegant and wonderful way?
Example Code:
//cropPict=method for cropping the picture in the relevant picturebox.
//CropSide=a little enum which tells the method which side to crop.
private void btnT_Click(object sender, EventArgs e)
{
cropPict(/*Reference to PictureBox Goes Here*/, CropSide.Top);
}
private void btnB_Click(object sender, EventArgs e)
{
cropPict(/*Reference to PictureBox Goes Here*/, CropSide.Bottom);
}
private void btnR_Click(object sender, EventArgs e)
{
cropPict(/*Reference to PictureBox Goes Here*/, CropSide.Right);
}
private void btnL_Click(object sender, EventArgs e)
{
cropPict(/*Reference to PictureBox Goes Here*/, CropSide.Left);
}
EDIT: As it happens, inspired by Hans below, rather than just stuffing the PictureBox into the tag. Which was a great idea I actually put a KeyValuePair into the tag for each button like so:
btnCCB.Tag = new KeyValuePair<CropSide,PictureBox>(CropSide.Bottom,pbxKoala);
btnCCL.Tag = new KeyValuePair<CropSide, PictureBox>(CropSide.Left, pbxKoala);
btnCCR.Tag = new KeyValuePair<CropSide, PictureBox>(CropSide.Right, pbxKoala);
btnCCT.Tag = new KeyValuePair<CropSide, PictureBox>(CropSide.Top, pbxKoala);
Then I could just wire all the buttons up to a single event handler like so:
private void btnC_Click(object sender, EventArgs e)
{
Button btnSend = (Button)sender;
KeyValuePair<CropSide, PictureBox> kvCrop = (KeyValuePair<CropSide, PictureBox>)btnSend.Tag;
cropPict(kvCrop.Value,kvCrop.Key);
}
Of course, there's still plenty more to do but that pretty much sorted out my problem. Thanks Hans!
Use the Button.Tag property to store a reference to its associated PictureBox. Cast sender to Button:
public Form1()
{
InitializeComponent();
button1.Tag = pictureBox1;
button1.Click += btnT_Click;
// etc..
}
private void btnT_Click(object sender, EventArgs e)
{
var btn = (Button)sender;
cropPict((PictureBox)btn.Tag, CropSide.Top);
}

Categories