Toggle the picture of picture box with picture box on_click c# - c#

I have one image in my picture box using the resource what i want is to change the image when i click the icon.png it should be changed to icon1.png within the picturebox then when i click again the picture box it should be changed to icon.png
private void pictureBox10_Click(object sender, EventArgs e)
{
if (pictureBox10.ImageLocation != #"icon1.png")
{
var image = Image.FromFile(#"icon1.png");
pictureBox10.Image = image;
}
if (pictureBox10.ImageLocation == #"icon1.png")
{
var image = Image.FromFile(#"icon.png");
pictureBox10.Image = image;
}
}
but its not working please help me out of this.

You're getting a null from the image location as it's not set when you're assigning a picture to the Image property. There are a few ways to fix this:
Change the assignment so you assign using the ImageLocation
pictureBox10.ImageLocation = #"icon1.png";
Change the check to see if the Image property is equal to your new Image
pictureBox10.Image == Image.FromFile(#"icon.png");
Set the image location at the same time you set the image property
pictureBox10.Image == Image.FromFile(#"icon.png");
pictureBox10.ImageLocation = #"icon.png" ;
I feel the second might not come back as equal, you probably want to try the first one or third
Suggested code:
private void pictureBox10_Click(object sender, EventArgs e)
{
if (pictureBox10.ImageLocation != #"icon1.png")
{
pictureBox10.ImageLocation = #"icon1.png"
}
if (pictureBox10.ImageLocation == #"icon1.png")
{
pictureBox10.ImageLocation = #"icon.png";
}
}
Or:
private void pictureBox10_Click(object sender, EventArgs e)
{
if (pictureBox10.ImageLocation != #"icon1.png")
{
var image = Image.FromFile(#"icon1.png");
pictureBox10.Image = image;
pictureBox10.ImageLocation = #"icon1.png";
}
if (pictureBox10.ImageLocation == #"icon1.png")
{
var image = Image.FromFile(#"icon.png");
pictureBox10.Image = image;
pictureBox10.ImageLocation = #"icon.png";
}
}
You would also need to update your intial property setting to set the ImageLocation and not the Image property or to set the ImageLocation at the same time you set the Image file
EDIT
Off the top of my head, to set the property initially, you can do this (Source):
protected override void OnLoad(EventArgs e){
pictureBox10.ImageLocation = #"icon.png";
}
Though I can't remember if the PictureBox would have been created then, if not then use the onShown event instead (Source)
EDIT 2
Here is another way to create the event and set the property, first follow the steps here to add the event onShown to the form. You need to click on the form itself and not the controls inside the form to find the event.
Once done, inside the event add the following code:
pictureBox10.ImageLocation = #"icon.png";
That should help to resolve your issue

Try to directly reference the picture into the picture box:
pictureBox10.Image = Image.FromFile(#"Images\a.bmp");
Source

Thanks every one thanks a lot but there was some problem but have solved this issue so m writing the actual code...
public Form1()
{
InitializeComponent();
pictureBox10.ImageLocation = #"icon.png";
}
private void pictureBox10_Click(object sender, EventArgs e)
{
if (pictureBox10.ImageLocation == #"icon1.png")
{
pictureBox10.ImageLocation = #"icon.png";
}
else
{
pictureBox10.ImageLocation = #"icon1.png";
}
}
first you have to initialize the image location then use two if condition i think that was the main problem use if else anyway thaks every one thaks a lot special thaks to #Draken

Related

C# UWP - How to get an object's drag image?

How can I get a copy of the drag image for an item I'm dragging in my application?
The DragStarting event contains a DragStartingEventArgs object that includes a DragUI object whose description is, "The visual representation of the data being dragged." Ideally, I'd like to do something like this within the DragStarting event:
private void OnDragStarting( UIElement sender, DragStartingEventArgs args )
{
// Create a new bitmap image object
var dragImage = new BitmapImage();
// Assign the drag image to the new bitmap image object
dragImage = args.DragUI. ????
}
However, there doesn't seem to be a way to get the drag image from the DragUI object. DragUI contains only "Set*" methods, and no "Get*" methods.
Is there a way to get the drag image as the drag operation begins?
You can use DataView.GetStorageItemsAsync() to receive the items you dragged into your applications.
<Grid AllowDrop="True" DragOver="Image_drop_drag_over_ui" Drop="image_drop"/>
//C# code
public async void Image_Drop(object sender, DragEventArgs e)
{
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
List<StorageFile> received_images = new List<StorageFile>();
var items = await e.DataView.GetStorageItemsAsync();
var storageFile = items[0] as StorageFile;
received_images.Add(storageFile);
}
}
private void Image_drop_drag_over_ui(object sender, DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Copy;
e.DragUIOverride.Caption = "Drop receipt";
e.DragUIOverride.IsCaptionVisible = true;
}

How to set or get a button background image (and or even button image)?

I have a Play button on my winform called btnplay. I set its background image by an image(play.png). I want when clicked btnplay its background image changed to another image (pause.png). I write this Conditional code but it don't work!
string customPath = "../img/";
string playImageFilename = "play.png";
if (btnplay.BackgroundImage == Image.FromFile(Path.Combine(customPath, playImageFilename)))
{...}
I suggest you using the Button Tag property for this. So, when you load the "play" background image, set the Tag property to "play", and when you change it to pause set the Tag to "pause"
So, on the button click event, you could do something like this:
string customPath = "../img/";
string playImageFilename = "play.png";
string pauseImageFilename = "pause.png";
if (btnplay.Tag.ToString() == "pause")
{
btnplay.BackgroundImage = Image.FromFile(Path.Combine(customPath, playImageFilename);
btnplay.Tag= "play";
}
else
{
btnplay.BackgroundImage = Image.FromFile(Path.Combine(customPath, pauseImageFilename);
btnplay.Tag= "pause";
}
Declare a variable outside of the btnPlay click event handler.
private bool playing = false;
private void btnPlay_Click(object sender, System.EventArgs e)
{
if (playing)
{
// set play image
}
else
{
// set pause image
}
playing = !playing;
}

Display picture only if textbox is blank

My Program: I have a textBox and a pictureBox (which contains an error picture, placed exactly next to textBox) in userControl in my program.
My Goal: I want to HIDE the picture in the pictureBox only if the user types text in the textBox. If the textBox is left blank, the image in the pictureBox should be shown.
I tried using errorProvider but I was totally lost because I am a newbie in C# Programming. There are many errorProvider examples online but all examples are using Form and I am trying to do it in UserControl. So, I thought I should try this method. Please can you help me with the code? Thanks for your help in advance.
ANSWER:
Sealz answer works! My program will be working offline. So, this one also works:
if (String.IsNullOrEmpty(textBox1.Text))
{
//Show Picture
pictureBox2.Visible = true;
}
else
{
//Hide Picture
pictureBox2.Visible = false;
}
Thanks everybody for looking at my question! You all are awesome. =)
You can use IsNullOrEmpty
if (String.IsNullOrEmpty(textBox1.Text))
{
//Show Picture
pictureBox1.ImageLocation = "locationofimg";
}
else
{
//Hide Picture
pictureBox1.ImageLocation = "";
}
To get fancy with it.
On form_Load() set the picturebox to nothing
private void Form1_Load(object sender, EventArgs e) {
pictureBox1.ImageLocation = "";
}
Then in the Textbox Change Method
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(textBox1.Text))
{
pictureBox1.ImageLocation = "";
}
else
{
pictureBox1.ImageLocation = "Image\Location.com.etc";
}
}
This will make the box empty to start with no image and as you type it will pop up. If the boxes text is deleted fully the image will vanish.
Just test if the textbox has any text, and set the property accordingly.
pictureBox1.ImageLocation = (textBox1.Text.Length > 0) ?
"imagefile" : String.Empty;
If this needs to update dynamically, just perform this action in the textbox's TextChanged event.

C# Need Help Changing a Label.Text - My Function/method won't change my Form Label Text

I wanted to make a function to organize my code better but this is giving me a headache, My Problem is that I want to use my Public Function to change the label.Text whenever I call them from the Form but it's not working. How can I get this working and please be very basic. Thank You.
Here's my Code:
namespace NoSleepHD
{
public partial class NoSleepHD : Form
{
public NoSleepHD()
{
InitializeComponent();
}
public void selectFolder(string driveLabel, string writePath)
{
FolderBrowserDialog Tree = new FolderBrowserDialog();
Tree.RootFolder = Environment.SpecialFolder.MyComputer;
Tree.ShowNewFolderButton = false;
Tree.Description = "Please Select any Drive OR Folder on Your External hard Drive";
Tree.ShowDialog();
if (Tree.SelectedPath.Length != 0)
{
driveLabel = Tree.SelectedPath.ToString();
Properties.Settings.Default.WritePath01 = driveLabel.ToString();
Properties.Settings.Default.Save();
}
}
private void Button1_Click(object sender, EventArgs e)
{
selectFolder(Label11.Text, Properties.Settings.Default.WritePath01);
}
but Label11.Text does not show any Text. Can someone kind please help me out. Thank You.
Of course it wouldn't: you're passing a string Label11.Text to the function. Pass the Label11 object only and set its text in the selectFolder function.
Ok. If you have 10 labels that are linked to folder locations, it would be better to do it this way (notice that the Click handler is using sender, so you can use this handler for all the label buttons, no need for separate handlers).
public void selectFolder(Label label, string writePath)
{
FolderBrowserDialog Tree = new FolderBrowserDialog();
Tree.RootFolder = Environment.SpecialFolder.MyComputer;
Tree.ShowNewFolderButton = false;
Tree.Description = "Please Select any Drive OR Folder on Your External hard Drive";
Tree.ShowDialog();
if (Tree.SelectedPath.Length != 0)
{
label.Text = Tree.SelectedPath.ToString();
Properties.Settings.Default.WritePath01 = label.Text;
Properties.Settings.Default.Save();
}
}
private void FolderSelector_Click(object sender, EventArgs e)
{
selectFolder(sender as Label, Properties.Settings.Default.WritePath01);
}

How to notify PropertyChanged after Image Opened?

I am having some problems with my gallery. First I download all thumbnails for an overview. But when I click on a picture, first I want to show the thumbnail and load the big picture. When this is done, I want to change the ImageSource to the new picture. Here is my example:
private BitmapImage picture;
public BitmapImage Picture
{
get
{
if (picture == null)
{
RequestBigpicture();
return Thumbnail;
}
return picture;
}
}
public void RequestBigpicture()
{
picture = new BitmapImage(new Uri("http://www.fun-hollywood.de/" + bigPicture, UriKind.Absolute));
picture.ImageOpened += pictureImage_ImageOpened;
}
void pictureImage_ImageOpened(object sender, System.Windows.RoutedEventArgs e)
{
NotifyPropertyChanged("Picture");
}
This was not working, also this part (as I read somewhere) in RequestBitPicture is not better:
picture = new BitmapImage(new Uri("http://www.fun-hollywood.de/" + bigPicture, UriKind.Absolute));
var pictureImage = new Image();
pictureImage.Source = picture;
pictureImage.ImageOpened += pictureImage_ImageOpened;
The ImageOpened is never called. How would be the best way to do this?
I think you should set the BitmapImage.CreateOptions property to None or to BackgroundCreation to instantly trigger the image download.
Because the default value is DelayCreation that is why your image won't donwloaded and the ImageOpened event is never fired.

Categories