Cannot delete file in C#.net windows application - c#

I am first creating Bitmap image file and saving it to some temp location. Later using that file to read in BitmapImage object to compare it with other file. Once comparison is done, I want to delete file but then it throws exception that file is being used by another process. How can I delete this file?
Here is my code:
private void btnLogin_Click(object sender, EventArgs e)
{
string strPath = AppDomain.CurrentDomain.BaseDirectory;
GC.Collect();
if (txtLoginImage.Text != "")
{
string strFileName = txtLoginImage.Text.Substring(txtLoginImage.Text.LastIndexOf('\\') + 1);
Bitmap MainImg = new System.Drawing.Bitmap(txtLoginImage.Text);
Bitmap NewImage = ConvertToGrayScale(MainImg);
NewImage.Save(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Temp\\" + strFileName, System.Drawing.Imaging.ImageFormat.Bmp);
NewImage.Dispose();
Uri SourceUri = new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Temp\\" + strFileName);
BitmapImage source = new BitmapImage();
source.UriSource = SourceUri;
IrisSystem.Class.BLL.User_BLL ubll = new IrisSystem.Class.BLL.User_BLL();
DataSet dsUserData= ubll.getlist();
bool isMatchFound = false;
if (dsUserData != null && dsUserData.Tables.Count > 0)
{
foreach (DataRow item in dsUserData.Tables[0].Rows)
{
Uri TargetUri = new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Grayscale\\" + item["GrayScaleImgName"]);
BitmapImage Target = new BitmapImage(TargetUri);
if (source.IsEqual(Target))
{
IrisSystem.frmHome frm= new IrisSystem.frmHome();
frm.strFullName = item["FullName"].ToString();
frm.ShowDialog();
Form.ActiveForm.Close();
isMatchFound = true;
break;
}
Target = null;
}
if (!isMatchFound)
MessageBox.Show("Invalid Credential..","Invalid Operation");
}
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Temp\\" + strFileName);
}
else
MessageBox.Show("Please select image", "Login Error");
}

You need to make sure that your Bitmap objects are disposed properly.
You are not disposing the MainImg object. you need to use using {} block to make sure that objects are disposed properly.
Replace This:
Bitmap MainImg = new System.Drawing.Bitmap(txtLoginImage.Text);
Bitmap NewImage = ConvertToGrayScale(MainImg);
NewImage.Save(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Temp\\"
+ strFileName, System.Drawing.Imaging.ImageFormat.Bmp);
NewImage.Dispose();
With This:
using(Bitmap MainImg = new System.Drawing.Bitmap(txtLoginImage.Text))
using(Bitmap NewImage = ConvertToGrayScale(MainImg))
{
NewImage.Save(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\Temp\\" +
strFileName, System.Drawing.Imaging.ImageFormat.Bmp);
}
EDIT:
Replace This:
BitmapImage source = new BitmapImage();
source.UriSource = SourceUri;
With This:
BitmapImage source = new BitmapImage();
source.BeginInit();
source.UriSource = SourceUri;
source.EndInit();

Related

How can i release Image memory?

I'm currently working on the code to take pictures with a Canon camera and float them on the image.
The problem right now is that the picture is displayed normally in the image, but the memory is allocated, but the memory is not released and keeps stacking.
The size of the picture is 6000 X 4000 and the picture is saved as JPG. Also, the capacity of the picture is 6MB.
How can I turn off the memory?
Below is a picture of the memory graph.
case 1:
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate {
var firstphoto = new BitmapImage();
var stream1 = File.OpenRead(MainWindow.PhotoPath + #"\IMG_" + photonumber + #".JPG");
firstphoto.BeginInit();
firstphoto.CacheOption = BitmapCacheOption.OnLoad;
firstphoto.StreamSource = stream1;
firstphoto.EndInit();
firstphoto.Freeze();
FirstPic.Source = firstphoto;
stream1.Close();
stream1.Dispose();
}));
break;
case 2:
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate {
var secondphoto = new BitmapImage();
var stream2 = File.OpenRead(MainWindow.PhotoPath + #"\IMG_" + photonumber + #".JPG");
secondphoto.BeginInit();
secondphoto.CacheOption = BitmapCacheOption.OnLoad;
secondphoto.StreamSource = stream2;
secondphoto.EndInit();
secondphoto.Freeze();
SecondPic.Source = secondphoto;
stream2.Close();
stream2.Dispose();
}));
break;
case 3:
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate {
var thirdphoto = new BitmapImage();
var stream3 = File.OpenRead(MainWindow.PhotoPath + #"\IMG_" + photonumber + #".JPG");
thirdphoto.BeginInit();
thirdphoto.CacheOption = BitmapCacheOption.OnLoad;
thirdphoto.StreamSource = stream3;
thirdphoto.EndInit();
thirdphoto.Freeze();
ThirdPic.Source = thirdphoto;
stream3.Close();
stream3.Dispose();
}));
break;
case 0:
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate {
var fourthphoto = new BitmapImage();
var stream4 = File.OpenRead(MainWindow.PhotoPath + #"\IMG_" + photonumber + #".JPG");
fourthphoto.BeginInit();
fourthphoto.CacheOption = BitmapCacheOption.OnLoad;
fourthphoto.StreamSource = stream4;
fourthphoto.EndInit();
fourthphoto.Freeze();
FourthPic.Source = fourthphoto;
stream4.Close();
stream4.Dispose();
}));

Why is not an image inserted in the RichTextBox?

I tried to insert image into RichTextBox by clicking the button.
I referred this post - How can I insert an image into a RichTextBox? and made the source code
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog f = new OpenFileDialog();
f.Filter = "BMP (*.bmp)|*.bmp|JPEG (*.jpg, *.jpeg, *.jpe, *.jfif)|*.jpg;*.jpeg;*.jpe;*.jfif|GIF (*.gif)|*.gif|PNG (*.png)|*.png|All files|*.bmp;*.jpg;*.jpeg;*.jpe;*.jfif;*.gif;*.png";
if (f.ShowDialog() == DialogResult.OK)
{
// Get type of the file
string typeOfFile = "";
Stack<char> typeParse = new Stack<char>();
int pointer = f.FileName.Length - 1
while (pointer > 0 && f.FileName[pointer] != '.')
typeParse.Push(f.FileName[pointer--]);
while (typeParse.Count > 0)
typeOfFile += typeParse.Pop();
typeOfFile = typeOfFile.ToLower();
// Convert Image to byte[]
Image image = Image.FromFile(f.FileName);
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
// Insert the image data in RichTextBox which is converted to hex from byte[]
// by using BitConverter.ToString(ms.ToArray();
StringBuilder imageRTF = new StringBuilder();
imageRTF.Append(#"{\pict\" + typeOfFile + #"blip\picw" + image.Width + #"\pich" + image.Height);
imageRTF.Append(#"\picwgoal" + image.Width + #"\pichgoal" + image.Height);
imageRTF.Append(#" " + BitConverter.ToString(ms.ToArray()) + #"}");
string rtf1 = richTextBox1.Rtf.Trim().TrimEnd('}');
string rtf2 = imageRTF.ToString();
richTextBox1.Rtf = rtf1 + rtf2;
}
}
but when I executed and opened png file, only one line break was inserted in RichTextBox..
What is wrong??

Image.Save Parameter not valid

I want to watermark files in a folder with another image. However, am getting the error
'Parameter Not Valid'
when invoking the code
img.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);
I have the following code;
public static string WatermarkImagesInFolder(string url)
{
if (url == null)
throw new Exception("URL must be provided");
string path = HttpContext.Current.Server.MapPath(url);
if (!Directory.Exists(path))
throw new DirectoryNotFoundException();
Directory.CreateDirectory(String.Format(#"{0}\watermarked", path));
List<string> urls = GetJpgFilesFromFolder(path);
foreach (string imageUrl in urls)
{
Image img = WatermarkImage(imageUrl);
string filename = Path.GetFileName(imageUrl);
string filepath = String.Format(#"{0}\watermarked\{1}", path, filename);
img.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
return "complete";
}
and
public static Image WatermarkImage(string filename)
{
using (Image image = Image.FromFile(filename))
using (Image watermarkImage = Image.FromFile(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["WatermarkImageUrl"])))
using (Graphics imageGraphics = Graphics.FromImage(image))
using (TextureBrush watermarkBrush = new TextureBrush(watermarkImage))
{
int x = (image.Width / 2 - watermarkImage.Width / 2);
int y = (image.Height / 2 - watermarkImage.Height / 2);
watermarkBrush.TranslateTransform(x, y);
imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(watermarkImage.Width + 1, watermarkImage.Height)));
return image;
}
}
as an example of input for img.save;
img.save("C:\\IMAGES\\wateremarked\\IMAGE (1).jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
any ideas on what could be causing the error here please?
In your WatermarkImage you have the image object in a using statement. As soon as that goes out of scope, Dispose is invoked on the image. You would need to return a copy of the image - or not use the using statement & ensure you dispose properly later.
See : Exception: Parameter is not valid (on passing new image to pictureBox)
public static string WatermarkImagesInFolder(string url)
{
if (url == null)
throw new Exception("URL must be provided");
string path = HttpContext.Current.Server.MapPath(url);
if (!Directory.Exists(path))
throw new DirectoryNotFoundException();
Directory.CreateDirectory(String.Format(#"{0}\watermarked", path));
List<string> urls = GetJpgFilesFromFolder(path);
foreach (string imageUrl in urls)
{
using(Image img = WatermarkImage(imageUrl))
{
string filename = Path.GetFileName(imageUrl);
string filepath = String.Format(#"{0}\watermarked\{1}", path, filename);
img.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
return "complete";
}
public static Image WatermarkImage(string filename)
{
Image image = Image.FromFile(filename);
using (Image watermarkImage = Image.FromFile(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["WatermarkImageUrl"])))
using (Graphics imageGraphics = Graphics.FromImage(image))
using (TextureBrush watermarkBrush = new TextureBrush(watermarkImage))
{
int x = (image.Width / 2 - watermarkImage.Width / 2);
int y = (image.Height / 2 - watermarkImage.Height / 2);
watermarkBrush.TranslateTransform(x, y);
imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(watermarkImage.Width + 1, watermarkImage.Height)));
return image;
}
}
Here is my alternative solution, which gives the ability to use picture stream, but not it's location as it's currently browsed from Kendo Control as in my example and it still have not physical location yet.
public static Image ApplyWatermark(HttpPostedFileBase img, string appDataPath)
{
Image resultImage = null;
using (Image image = Image.FromStream(img.InputStream))
{
using (Image watermarkImage = Image.FromFile(appDataPath + "\\Watermark\\sample-watermark.png"))
{
using (Graphics imageGraphics = Graphics.FromImage(image))
{
using (Brush watermarkBrush = new TextureBrush(watermarkImage))
{
imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(0, 0), image.Size));
resultImage = (Image)image.Clone();
}
}
}
}
return resultImage;
}
private void SaveImagesOnDisk(IEnumerable<HttpPostedFileBase> images, int rentalPropertyId)
{
if (images != null)
{
foreach (var file in images)
{
// Some browsers send file names with full path. This needs to be stripped.
var fileName = Path.GetFileName(file.FileName);
var appDataPath = this.Server.MapPath("~/App_Data/");
var newPath = Path.Combine(appDataPath, rentalPropertyId.ToString());
if (!Directory.Exists(newPath))
{
DirectoryInfo di = Directory.CreateDirectory(newPath);
}
var physicalPath = Path.Combine(newPath, fileName);
System.Drawing.Image markedImage = Helper.ApplyWatermark(file, appDataPath);
markedImage.Save(physicalPath, System.Drawing.Imaging.ImageFormat.Png);
}
}
//return this.Json(new { status = "OK" }, "text/plain");
}

Check if the image resource is null

I Working on Windows Phone 8 application.
string Image = "/MyData/" + myObject.ImageName + "big.png";
BitmapImage bmp = new BitmapImage(new Uri(Image , UriKind.Relative));
MyImage.Source = bmp;
I have a image in the folder MyData/, i have 2 sets of images like <imagename>big.png,<imagename>small.png.
So here what is happening is i want to check if <imagename>big.png exists in the location or not, if not pick <imagename>small.png.
How to do it ?
EDIT
I solved it myself, here is how.
File.Exists("path to file") here path should be `folderName/filenames` and not `/folderName/filenames`
Thanks for everyone who helped me.
string image = "/MyData/" + myObject.ImageName + "/big.png";
string fileName = Path.GetFileName(image);
if(!string.IsNullOrEmpty(fileName))
{
MessageBox.Show("File Exist -"+fileName);
}
else
{
MessageBox.Show("No File Exist -");
}
BitmapImage bmp = new BitmapImage(new Uri(image , UriKind.Relative));
if(bmp==null)
{
image = "/MyData/" + myObject.ImageName + "/small.png";
bmp = new BitmapImage(new Uri(image , UriKind.Relative));
}
MyImage.Source = bmp;

Release a file being used by another process

I'm implementing "copy map to clip" as an image.
what I do is I create an image an save it to certain directory and copy it to clipboard.
but before I do that I delete every file that exist in the directory, but now I cant since, the image is being used by clipboard.
here's my code.
public override void OnClick()
{
//base.OnClick();
DeleteOldCopiedJPG();
System.Windows.Forms.Clipboard.Clear();
string fileName = System.Windows.Forms.Application.ExecutablePath.Substring(0, System.Windows.Forms.Application.ExecutablePath.LastIndexOf("\\") + 1) + Guid.NewGuid() + ".jpg";
//System.Windows.Forms.Cursor.Current = Cursors.Wait;
//if (System.IO.File.Exists(fileName))
// System.IO.File.Delete(fileName);
IExport objExport = (IExport)new ExportJPEG();
objExport.ExportFileName = fileName;
#if Debug || Release
ESRI.ArcGIS.Display.tagRECT objExportRECT = default( ESRI.ArcGIS.Display.tagRECT);
#else
tagRECT objExportRECT = default(tagRECT);
#endif
var _with1 = objExportRECT;
_with1.left = mapControl.ActiveView.ExportFrame.left;
_with1.top = mapControl.ActiveView.ExportFrame.top;
_with1.right = mapControl.ActiveView.ExportFrame.right;
_with1.bottom = mapControl.ActiveView.ExportFrame.bottom;
IEnvelope envelope = new EnvelopeClass();
envelope.PutCoords(mapControl.ActiveView.ExportFrame.left, mapControl.ActiveView.ExportFrame.top,
mapControl.ActiveView.ExportFrame.right, mapControl.ActiveView.ExportFrame.bottom);
objExport.PixelBounds = envelope;
System.Int32 intHDC = objExport.StartExporting();
mapControl.ActiveView.Output(intHDC, Convert.ToInt16(objExport.Resolution), objExportRECT, null, null);
objExport.FinishExporting();
objExport.Cleanup();
System.Drawing.Image objImage = System.Drawing.Bitmap.FromFile(fileName);
System.Windows.Forms.Clipboard.SetImage(objImage);
//RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32);
//renderTargetBitmap.Render((Visual)mapControl.ActiveView.ScreenDisplay);
//Clipboard.SetImage(renderTargetBitmap);
}
private void DeleteOldCopiedJPG(string Path)
{
string[] filePaths = Directory.GetFiles(System.Windows.Forms.Application.ExecutablePath.Substring(0, System.Windows.Forms.Application.ExecutablePath.LastIndexOf("\\") + 1));
foreach (string filepath in filePaths)
if (filepath.Substring(filepath.Length - 4) == ".jpg")
try{ File.Delete(filepath); } catch {}
}
You need to dispose the image after inserting it into the clipboard:
using (System.Drawing.Image objImage = System.Drawing.Bitmap.FromFile(fileName))
{
System.Windows.Forms.Clipboard.SetImage(objImage);
}
Otherwise it will remain open until the garbage collector calls the finalizer for objImage.

Categories