Deleting Image that's in PictureBox - c#

I've searched and read a lot of the different ways to do this... and tried many of them.
When the program loads it loads printmark.png into the picture box.
Of course every time I try to delete the PNG file it says it's in use. As you can see I've tried both the Image.FileFrom and the picturebox.Load method.
This is the code I have.
private void GetCurrentLogos()
{
Image CurrentWM = Image.FromFile(#"C:\pics\logo.png");
Image CurrentPM = Image.FromFile(#"C:\pics\printmark.png");
pbWatermark.Image = CurrentWM;
pbPrintmark.Image = CurrentPM;
//pbWatermark.Load(#"C:\pics\logo.png");
//pbPrintmark.Load(#"C:\pics\printmark.png");
}
private void btnSetPM_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
ListViewItem item = listView1.SelectedItems[0];
txtNewPM.Text = item.Tag.ToString();
pbPrintmark.Image.Dispose();
pbPrintmark.Image = null;
pbPrintmark.Refresh();
Application.DoEvents();
renameMark("printmark.png", txtNewPM.Text);
}
}
private void renameMark(string MarkType, string FileName)
{
string path = txtPath.Text;
string FullSource = path + FileName;
string FullDest = path + MarkType;
if(File.Exists(FullDest))
{
File.Delete(FullDest);
}
System.IO.File.Copy(FullSource, FullDest);
}

See Image.FromFile():
The file remains locked until the Image is disposed.
To get around this, pass the returned image to a new Bitmap so that the original lock gets released:
Image tmp = Image.FromFile(#"C:\pics\logo.png");
Image CurrentWM = new Bitmap(tmp);
tmp.Dispose();

As noted in some of the answers to this question, an image created by Image.FromFile keeps the underlying file open. This is by design, as the MSDN documentation says ("The file remains locked until the Image is disposed."). You can get around this by loading the file into a MemoryStream, then creating the image from that stream.

tmp.Dispose(); didnt worked for me so maybe u need also a different solution.
I used it for my dataGridView_SelectionChanged:
private void dataGridViewAnzeige_SelectionChanged(object sender, EventArgs e)
{
var imageAsByteArray = File.ReadAllBytes(path);
pictureBox1.Image = byteArrayToImage(imageAsByteArray);
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}

Related

How to fix The process cannot access the file because it is being used by another process

I wrote a small code to make a slideshow program on a different screen. Everything is running fine also the delete and copy part, unless I do it right after the first picture is shown. If I do it later the debugger steps in.
private void timer1_Tick(object sender, EventArgs e)
{
string[] images = Directory.GetFiles(#"D:\reflexscherm\root\Logo-teams2", "*.*");
counter++;
var maxcount = images.Count();
textBox1.Text = maxcount.ToString();
if (counter > maxcount - 1)
{
counter = 0;
maxcount = images.Count();
}
//pb1.Image.Dispose();
pb1.Image = Image.FromFile(images[counter]);
//Image oldImage = pb1.Image;
//pb1.Image.Dispose();
//oldImage.Dispose();
//pb1.Image = Image.FromFile(images[counter]);
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
Image oldImage = pb1.Image;
pb1.Image = Image.FromFile(#"D:\reflexscherm\root\sponsor1\x. Groot-logo-REFLEX.jpg");
pb1.Image.Dispose();
oldImage.Dispose();
string[] files = System.IO.Directory.GetFiles(sourcepath);
string[] delfiles = Directory.GetFiles(targetpath);
this.Hide();
foreach (string d in delfiles)
{
Image oldI = pb1.Image;
pb1.Image = Image.FromFile(#"D:\reflexscherm\root\sponsor1\x. Groot-logo-REFLEX.jpg");
//pb1.Image.Dispose();
oldI.Dispose();
File.Delete(d);
}
foreach (string s in files)
{
string fname = s.Substring(sourcepath.Length + 1);
File.Copy(Path.Combine(sourcepath, fname), Path.Combine(targetpath, fname), true);
this.Show();
timer1.Start();
}
What I am looking for is some help to adjust the code, so when I change files in sourcefolder then program copies the files from the sourcefolder to the targetfolder. I know how to use filewatcher. I am using a button to test the code.
Method 1: Preserve a copy and assign a Control's property
Use this method when a Bitmap object is handled in more than one place, so we need to preserve it for further elaborations/assignments and save to disc after.
Assign, store and dispose of the source Bitmap immediately, deleting the Image file:
Bitmap bitmap = null;
//---------------------------------------------
string imagePath = #"[Path of the Image]";
bitmap?.Dispose();
pictureBox1.Image?.Dispose();
using (Bitmap tempImage = new Bitmap(imagePath, true))
{
bitmap = new Bitmap(tempImage);
pictureBox1.Image = bitmap;
}
File.Delete(imagePath);
Method 2: Assign the Bitmap and dispose of it immediately
This method can be used when you need to assign a Bitmap to a Control and then move/delete the Image file. The Image is disposed of immediately, so it's only available through a Control's property: if we ask to have it back, sometimes what we get is not exactly what we gave.
string imagePath = #"[Path of the Image]";
using (Image image = Image.FromFile(imagePath, true))
{
pictureBox1.Image?.Dispose();
pictureBox1.Image = new Bitmap(image);
}
File.Delete(imagePath);
Note that the old image assigned to the Control, if any, is disposed of before assigning a new one.
Also note I'm always specifying to preserve the internal ICM informations, if any, specifying true as the second parameter of both new Bitmap(imagePath, true) and Image.FromFile(imagePath, true).
Some images won't look as the original if we don't.
You should use a readonly File Access if you don't want it to be locked:
using( FileStream stream = new FileStream( path, FileMode.Open, FileAccess.Read ) )
{
image = Image.FromStream( stream );
}
Hope it helps...
I had the same problem before, i did the following:
this.photo.Dispose();
this.photo.Refresh();
this.photo.Image.Dispose();
this.photo.Image = null;
this.photo.ImageLocation = null;
and it worths it.

Delete or have Control of an Image while your Application is Using it C#

I’m using WinForms. In my form I have a picturebox. On form load, my program opens an image document into my picturebox from my C:/image directory. The problem is when my program opens that image I cannot go into my C:/image directory and delete this picture because my application is using it. When I go to C:/image directory and try to delete the picture I get this error.
My goal is to have control over the image document that means i have the ability to delete the specific document even if its being used by my application.
Test: I tested if you can delete an image while viewing it at the same time with "Windows Photo Viewer" installed into in my computer, and that application lets you. Windows photo viewer doesn't lock the images. When you delete an image from the directory the image goes away in windows photo viewer as well. I want to accomplish something similar.
Suggested code: I tried implementing this but, i think i'm implementing it incorrectly.
Image img;
using (var bmpTemp = new Bitmap("image_file_path"))
{
img = new Bitmap(bmpTemp);
}
Below i provided my code i wrote to load the picture into my picture box.
private void Form1_Load(object sender, EventArgs e) //When form load you do this:
{
try // Get the tif file from C:\image\ folder
{
string path = #"C:\image\";
string[] filename = Directory.GetFiles(path, "*.tif"); //gets a specific image doc.
pictureBox1.Load(filename[0]);
lblFile.Text = filename[0];
RefreshImage(); // refreshing and showing the new file
opened = true; // the files was opened.
Image img1 = Image.FromFile(lblFile.Text);
pictureBox1.Image = img1;
pictureBox1.Width = img1.Width;
pictureBox1.Height = img1.Height;
picWidth = pictureBox1.Width;
picHeight = pictureBox1.Height;
getRatio();
}
catch (Exception ex)
{
MessageBox.Show("No files or " + ex.Message);
}
}
Make a copy of the image file bits before creating the image:
private void Form1_Load(object sender, EventArgs e) //When form load you do this:
{
try // Get the tif file from C:\image\ folder
{
string path = #"C:\image\";
string[] filename = Directory.GetFiles(path, "*.tif"); //gets a specific image doc.
FileInfo fi = new FileInfo(filename[0]);
byte [] buff = new byte[fi.Length];
using ( FileStream fs = File.OpenRead(fileToDisplay) )
{
fs.Read(buff, 0, (int)fi.Length);
}
MemoryStream ms = new MemoryStream(buff);
Bitmap img1 = new Bitmap(ms);
opened = true; // the files was opened.
pictureBox1.Image = img1;
pictureBox1.Width = img1.Width;
pictureBox1.Height = img1.Height;
picWidth = pictureBox1.Width;
picHeight = pictureBox1.Height;
getRatio();
}
catch (Exception ex)
{
MessageBox.Show("No files or " + ex.Message);
}
}

How to save image jpg or other format after uploading as png using asp.net c#

I have asp:FileUpload and I want to save all uploaded images of all acceptable formats as png images in the Images forlder on website, my upload code is:
protected void btnSave_Click(object sender, EventArgs e)
{
if (fup2.HasFile)
{
Regex reg = new Regex(#"(?i).*\.(gif|jpe?g|png|tif)$");
string uFile = fup2.FileName;
if (reg.IsMatch(uFile))
{
string saveDir = Server.MapPath(#"~/Images/");
string SavePath = saveDir + uFile;
fup2.SaveAs(SavePath);
}
else
{
Response.Write("Error");
}
}
}
I also tried using
var tempImg = Image.FromFile(Server.MapPath(#"~/Images/"));
tempImg.Save("a.tiff", ImageFormat.png);
It keeps throwing file not found exception
Any new ideas?
Use Bitmap.FromStream. Something like:
using System.Drawing;
protected void btnSave_Click(object sender, EventArgs e)
{
if (fup2.HasFile)
{
Regex reg = new Regex(#"(?i).*\.(gif|jpe?g|png|tif)$");
string uFile = fup2.FileName;
if (reg.IsMatch(uFile))
{
string saveDir = Server.MapPath(#"~/Images/");
string SavePath = saveDir + Path.GetFileName(uFile) + ".png";
Bitmap b = (Bitmap)Bitmap.FromStream(fup2.PostedFile.InputStream);
b.Save(SavePath, ImageFormat.Png);
}
else
{
Response.Write("Error");
}
}
}
The Image.FromFile -> Save should do the trick, but i don't see how you are using the correct path - you just point at the directory and not the actual file when calling FromFile
As a side note, doing this processing on a web processing thread is not a great idea but for small load it could work.

Delete an image being used by another process

I am writing a winform application in C# to open an image and overlay another image on top of it.
The bottom image is a .jpg and the top one is a .bmp converted from .svg. The .jpg and the .svg are the only ones I want to keep in the folder. The .bmp works as a temp.
I was using the following code to overlay the images. But I am having trouble to delete the temp .bmp as it is used by another process. I think it is this combine code still have access to the last .bmp file.
Could anyone help me on this? Thanks.
private void buttonSearch_Click(object sender, EventArgs e)
{
FailInfo.Text = "";
deletebmp(strFolderPath);
...
// Check if the specified front image exists. Yes, show the file name and convert SVG to BMP. No, show the error msg.
if (File.Exists(strFilePathF))
{
labelFront.Text = strFileNameF;
var svgConvert = SvgDocument.Open(svgFilePathF);
svgConvert.Draw().Save(bmpFilePathF);
pictureBoxFront.Image = Image.FromFile(strFilePathF);
}
else
{
labelFront.Text = "Couldn't find the file!";
pictureBoxFront.Image = null;
}
// Check if the specified back image exists. Yes, show the file name and convert SVG to BMP. No, show the error msg.
if (File.Exists(strFilePathBF))
{
labelBack.Text = strFileNameBF;
strFilePathB = strFilePathBF;
pictureBoxBack.Image = Image.FromFile(strFilePathB);
labelResult.Text = "FAIL";
labelResult.BackColor = Color.FromArgb(255, 0, 0);
var svgConvert = SvgDocument.Open(svgFilePathBF);
bmpFilePathB = strFolderPath + strFileNameBF + ".bmp";
svgConvert.Draw().Save(bmpFilePathB);
svgFilePathB = svgFilePathBF;
inspectionres(svgFilePathB);
labelreason.Visible = true;
}
else if (File.Exists(strFilePathBP))
{
labelBack.Text = strFileNameBP;
strFilePathB = strFilePathBP;
pictureBoxBack.Image = Image.FromFile(strFilePathB);
labelResult.Text = "PASS";
labelResult.BackColor = Color.FromArgb(0, 255, 0);
var svgConvert = SvgDocument.Open(svgFilePathBP);
bmpFilePathB = strFolderPath + strFileNameBP + ".bmp";
svgConvert.Draw().Save(bmpFilePathB);
svgFilePathB = svgFilePathBP;
inspectionres(svgFilePathB);
labelreason.Visible = false;
}
else
{
labelBack.Text = "Couldn't find the file!";
pictureBoxBack.Image = null;
labelResult.Text = "ERROR";
labelResult.BackColor = Color.FromArgb(0, 255, 255);
labelreason.Visible = false;
}
}
//
// Overlay the SVG file on top of the JPEG file
//
private Bitmap Combine(string jpegFile, string bmpFile)
{
Image image1 = Image.FromFile(jpegFile);
Image image2 = Image.FromFile(bmpFile);
Bitmap temp = new Bitmap(image1.Width, image1.Height);
using (Graphics g = Graphics.FromImage(temp))
{
g.DrawImageUnscaled(image1, 0, 0);
g.DrawImageUnscaled(image2, 0, 0);
}
return temp;
}
//
// Show the overlaid graphic in the picturebox
//
private void checkBoxOverlay_CheckedChanged(object sender, EventArgs e)
{
try
{
if (FindFront)
if (checkBoxOverlay.Checked)
pictureBoxFront.Image = Combine(strFilePathF, bmpFilePathF);
else
pictureBoxFront.Image = Image.FromFile(strFilePathF);
else
pictureBoxFront.Image = null;
if (FindBack)
if (checkBoxOverlay.Checked)
pictureBoxBack.Image = Combine(strFilePathB, bmpFilePathB);
else
pictureBoxBack.Image = Image.FromFile(strFilePathB);
else
pictureBoxBack.Image = null;
}
catch (Exception ex)
{
MessageBox.Show("Error loading image" + ex.Message);
}
}
//
// Option of changing the image folder
//
private void buttonPath_Click(object sender, EventArgs e)
{
FolderBrowserDialog FolderBrowserDialog1 = new FolderBrowserDialog();
if (FolderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
strFolderPath = FolderBrowserDialog1.SelectedPath + "\\";
}
}
//
// Pull the inspection result info from the SVG file
//
private void inspectionres(string filename)
{
XDocument document = XDocument.Load(filename);
XElement svg_Element = document.Root;
string sb = null;
var faillist = (from svg_path in svg_Element.Descendants("{http://www.w3.org/2000/svg}text") select svg_path).ToList();
foreach (var item in faillist)
{
sb += item.ToString();
}
}
//
// Delete all the .bmp files generated from .svg files
//
private void deletebmp(string path)
{
// Unload the images from the picturebox if applicable
pictureBoxFront.Image = null;
pictureBoxBack.Image = null;
string[] files = Directory.GetFiles(path, "*.bmp");
for (int i = 0; i < files.Length; i ++ )
File.Delete(files[i]);
}
}
Image implements IDisposable, so simply setting the pictureBox.Image property to null will not release resources (in your case, the file). Your Combine method also leaves the images open. You have to call Dispose before attempting to delete the file:
Image image1 = Image.FromFile(path1);
File.Delete(path1); // error - file is locked
Image image2 = Image.FromFile(path2);
image2.Dispose();
File.Delete(path2); // works
An alternative approach (and I assume you're using WinForms here, in WPF it's a little different) would be to load the bitmap from the file manually (using FromStream). Then, you can close the stream immediately and delete the file:
Image image;
using (Stream stream = File.OpenRead(path))
{
image = System.Drawing.Image.FromStream(stream);
}
pictureBox.Image = image;
File.Delete("e:\\temp\\copy1.png"); //works
Vesan's answer didn't helped me so I found an different solution.
So I can safe/open an image and if I want instantly delete the image.
i used it for my dataGridView_SelectionChanged:
private void dataGridViewAnzeige_SelectionChanged(object sender, EventArgs e)
{
var imageAsByteArray = File.ReadAllBytes(path);
pictureBox1.Image = byteArrayToImage(imageAsByteArray);
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
All above answers are perfectly fine, but I've got a different approach.
Using Image abstract class, you will not get quite a lot of options for manipulating and resizing image.
Rather you do as follows:-
Bitmap img = new Bitmap(item);
img.SetResolution(100, 100);
Image imgNew = Image.FromHbitmap(img.GetHbitmap());
pictureBox1.Image = imgNew;
img.Dispose();

How to pass a photo between pages in Windows Phone?

I know how to pass data through
NavigationService.Navigate(new Uri("/Second.xaml?msg=mesage", UriKind.Relative));
The question is, how can I pass an image selected from the library to another page?
To select an image, I use the PhotoChooserTask and in the event where it is completed I have this:
private void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.ChosenPhoto != null)
{
BitmapImage image = new BitmapImage();
image.SetSource(e.ChosenPhoto);
this.img.Source = image;
}
}
How can I send the chosen photo to another page? Do I have to write it in a buffer, set a global variable or 'save' it in Isolated storage?
You could save your picture in IsolatedStorage first, pass the file path to another page as a string parameter, load the picture out when you need it.
Use PhoneApplicationService to save the image into State, load it when you need it.
Sample for saving into IsolatedStorage:
public static void SaveStreamToStorage(Stream imgStream, string fileName)
{
using (IsolatedStorageFile iso_storage = IsolatedStorageFile.GetUserStoreForApplication())
{
//Structure the path you want for your file.
string filePath = GetImageStorePathByFileName(fileName);
//Constants.S_STORE_PATH is the path I want to store my picture.
if (!iso_storage.DirectoryExists(Constants.S_STORE_PATH))
{
iso_storage.CreateDirectory(Constants.S_STORE_PATH);
}
//I skip the process when I find the same file.
if (iso_storage.FileExists(filePath))
{
return;
}
try
{
if (imgStream.Length > 0)
{
using (IsolatedStorageFileStream isostream = iso_storage.CreateFile(filePath))
{
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(imgStream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
// Encode WriteableBitmap object to a JPEG stream.
Extensions.SaveJpeg(wb, isostream, wb.PixelWidth, wb.PixelHeight, 0, 100);
isostream.Close();
bitmap.UriSource = null;
bitmap = null;
wb = null;
}
}
}
catch(Exception e)
{
if (iso_storage.FileExists(filePath))
iso_storage.DeleteFile(filePath);
throw e;
}
}
}
Sample for reading picture from IsolatedStorage:
public static BitmapImage LoadImageFromIsolatedStorage(string imgName)
{
try
{
var bitmapImage = new BitmapImage();
//bitmapImage.CreateOptions = BitmapCreateOptions.DelayCreation;
using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
{
//Check if file exists to prevent exception when trying to access the file.
if (!iso.FileExists(GetImageStorePathByFileName(imgName)))
{
return null;
}
//Since I store my picture under a folder structure, I wrote a method GetImageStorePathByFileName(string fileName) to get the correct path.
using (var stream = iso.OpenFile(GetImageStorePathByFileName(imgName), FileMode.Open, FileAccess.Read))
{
bitmapImage.SetSource(stream);
}
}
//Return the picture as a bitmapImage
return bitmapImage;
}
catch (Exception e)
{
// handle the exception
Debug.WriteLine(e.Message);
}
return null;
}
You could use a variable defined in your app.xaml.cs and call it from your other page like so (don't mind the variable names, just a code sample I use for language support):
private LanguageSingleton LanguageInstance
{
get
{
return (App.Current as App).Language;
}
}
Here is how you could define that variable:
public LanguageSingleton Language { get; set; }
I'm sure there are more ways in doing this but this is one solution.

Categories

Resources