Replace image with new image - c#

when i try to delete the image i get this error i tried to dispose of the picture box but that doesn't seem to work.
System.IO.IOException: 'The process cannot access the file
'C:\Users*******\Documents\Visual Studio
2019\Project****************\dbo\DBImages\Bolgheri Sassicaia.jpg'
because it is being used by another process.'
the image i load from this folder, i want to delete it and then add another picture with the same name.
pretty much i want to update the image.
This is where i load the image to the picture box
if (imagepathtext.TextLength > 0)
{
image1 = Image.FromFile(imagepathtext.Text);
drinkImageView.Image = image1;
}
This is where i try to replace the image.
private void UpdatebtnClick(object sender, EventArgs e)
{
if (imagepath.TextLength > 9)
{
DialogResult result = MessageBox.Show("Are you also trying to
update image?", "Confirmation", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
image1.Dispose();
System.IO.File.Delete(imagepathtext.Text);
}
}
}

You have to dispose image1 object to close file handle. This is actual object keeping handle to your file.
Before this call
System.IO.File.Delete(imagepathtext.Text);
make sure to dispose image1 object
image1.Dispose();

So i figured it out after long trials and error.
We have to dispose of the image before loading the new image.
like so
private void BrowseImage_Click(object sender, EventArgs e)
{
DrinkView.Image.Dispose();
var imagePath = image.BROWSEIMAGE();
if (imagePath.Length > 10)
{
DrinkView.Image = new Bitmap(imagePath);
imagepath.Text = imagePath;
}
}

Related

Toggle the picture of picture box with picture box on_click 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

How to delete file and that file is used by another process using C#

In my C# application I want to delete file in below scenario.
OpenFileDialog and select any .jpg file.
Display that file in PictureBox.
Delete that file if needed.
I already try while doing step 3 I set default Image to PictureBox just before delete but that is not work.
How can I delete file? Please suggest me.
// Code for select file.
private void btnSelet_Click(object sender, EventArgs e)
{
if (DialogResult.OK == openFileDialog1.ShowDialog())
{
txtFileName.Text = openFileDialog1.FileName;
myPictureBox.Image = Image.FromFile(openFileDialog1.FileName);
}
}
// Code for Delete file
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
//myPictureBox.Image = Image.FromFile(System.IO.Directory.GetCurrentDirectory() + #"\Images\defaultImage.jpg");
System.IO.File.Delete(txtFileName.Text);
MessageBox.Show("File Delete Sucessfully");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Replacing the image sounds like a good idea - but don't forget to dispose of the old Image that's still holding the file open (and will, by default, until that Image is garbage collected - at some unknown time in the future):
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
var old = myPictureBox.Image;
myPictureBox.Image = Image.FromFile(System.IO.Directory.GetCurrentDirectory() + #"\Images\defaultImage.jpg");
old.Dispose();
System.IO.File.Delete(txtFileName.Text);
MessageBox.Show("File Delete Sucessfully");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
(It may also be possible to Dispose of the Image directly without replacing the image for the PictureBox - it depends on what else you're going to do after deletion - e.g. if the form on which the PictureBox appears is closing that you may want to let that happen first and then just directly dispose of the image).

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

Allow users to select and change icons in a multi company program

I am developing a C# program that will allow the users to enter multiple companies, and I am trying to allow the users to use an icon (as well as the name of the company in the taskbar) for each company to help them differ easily between them.
So far I have the following code, which shows a picturebox of the icon to confirm it is the correct image, but when I run the program it crashes with an out-of-memory-exception after changing the image 3 or 4 times. After reading solutions to similar questions on this site I have tried to dispose the image but can't seem to get it to work correctly:
private void btnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = "R:\\rightsort fulfilment\\charity icons";
openFileDialog1.Title = "Choose an Icon";
openFileDialog1.FileName = "";
openFileDialog1.Filter = "Icon Files|*.ico";
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
txtIcon.Text = openFileDialog1.FileName;
pictureBox1.Image = Image.FromFile(txtIcon.Text);
pictureBox1.Visible = true;
}
else
{
txtIcon.Text = "";
pictureBox1.Visible = false;
}
}
Any help would be greatly appreciated!
Image class inherited from IDisposable. You need to call Dispose method every time you replace it with new image to release resources.
However I don't know if this is the source of your problem. You also can try to run memory profiler and see where and how exactly memory gets allocated.
I would try disposing the old image in the picture control. Something like this:
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
txtIcon.Text = openFileDialog1.FileName;
Image oldImage = pictureBox1.Image;
pictureBox1.Image = Image.FromFile(txtIcon.Text);
if (oldImage != null)
{
oldImage.Dispose();
}
pictureBox1.Visible = true;
}

Save Image to particular folder from Openfiledialog?

I want to save images to my project folder from openfiledialog result . I don't get foler path to save . How do I get folder path ? How do I save that ? Please Help me.
FileDialog.FileName gives the full path to the file. And btw it is probably easier to use the SaveFileDialog because you want to save something, not open.
Hello thinzar,
private void button2_Click(object sender, EventArgs e)
{
Bitmap myBitmap = new Bitmap();
this.saveFileDialog1.FileName = Application.ExecutablePath;
if (this.saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
myBitmap.Save(this.saveFileDialog1.FileName);
}
}
Bye
The System.Windows.Forms.FolderBrowserDialog allows the user to select a folder. Maybe that would be a better option?
Something alone these lines
Bitmap myImage = new Bitmap();
// draw on the image
SaveFileDialog sfd = new SaveFileDialog ();
if(sfd.ShowDialog() == DialogResult.OK)
{
myImage.Save(sfd.FileName);
}
I guess you're opening a file elsewhere and then using the results to later save stuff into the directory you opened from?
DialogResult result = OpenFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string directoryName = Path.GetDirectoryName(OpenFileDialog1.FileName);
// directoryName now contains the path
}

Categories