如何从特定文件夹路径中删除图像文件?

时间:2018-06-04 08:58:34

标签: c# winforms

我创建了两个图像,第一个是条形码图像,第二个是SkuImage,之后我在第三个图像中合并它(最终图像)。成功合并后 我想删除特定文件夹中的条形码图像和Sku图像,但是当我尝试删除图像文件时,它会给我一个错误,即"该进程无法访问文件\ Path \,因为它正被另一个进程使用&#34 ;。 在删除之前我处理了这样的图像" SkuImage.Dispose()"但它不会删除。我该如何删除?

guest user permissions with no limitation

3 个答案:

答案 0 :(得分:0)

您应该处理实现IDisposable的每个类。然后,您可以删除图像文件。

答案 1 :(得分:0)

               string Filename = "Barcodeimage" + i + ".jpeg";



                string imgsavepath = "E:\\Pankaj\\BarcodeDemo\\BarcodeDemo\\BarcodeImage\\" + "BarcodeImg.jpeg";

                string imgSkupath = "E:\\Pankaj\\BarcodeDemo\\BarcodeDemo\\BarcodeImage\\" + "Sku.jpeg";
                BarcodeLib.SaveTypes savetype = BarcodeLib.SaveTypes.UNSPECIFIED;
                savetype = BarcodeLib.SaveTypes.JPG;
                System.IO.FileStream MemStream = new FileStream(imgsavepath, FileMode.Create, FileAccess.Write);

                System.Drawing.Image barcodeImage = null;
                //Bitmap FinalImage = null;
                BarcodeLib.Barcode b = new BarcodeLib.Barcode();


                //b.IncludeLabel = true;
                b.LabelFont = new Font("Arial", 5);

                string sku = "SKU:VXN4214IN";
                string StringToEncode = "16280/" + i + ' ' + sku;

                Image img = new Bitmap(130, 50);  //final image

                Graphics g = Graphics.FromImage(img);


                barcodeImage = b.Encode(BarcodeLib.TYPE.CODE128, "001234", Color.Black, Color.White, 113, 18);



                Bitmap SkuImage = new Bitmap(113, 18, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                RectangleF rectf = new RectangleF(10, 5, 113, 18);
                Graphics graphics = Graphics.FromImage(SkuImage);
                // SkuImage.SetPixel(10,10,Color.Blue);
                graphics.DrawString(StringToEncode, new Font("Arial", 4), Brushes.Black, rectf);



                b.SaveImage(MemStream, savetype);
                MemStream.Close();
                barcodeImage.Dispose();

                SkuImage.Save(imgSkupath);
                graphics.Dispose();
                SkuImage.Dispose();

                g.Clear(Color.White);   //here change BG color of Image



                System.IO.FileStream fileStream1 = new FileStream("E:\\" + @"Pankaj/BarcodeDemo/BarcodeDemo/BarcodeImage/BarcodeImg.jpeg", FileMode.Open, FileAccess.Read);

                System.IO.FileStream fileStream2 = new FileStream("E:\\" + @"Pankaj/BarcodeDemo/BarcodeDemo/BarcodeImage/Sku.jpeg", FileMode.Open, FileAccess.Read);


                g.DrawImage(Image.FromStream(fileStream1), new Point(15, 15));
                g.DrawImage(Image.FromStream(fileStream2), new Point(25, 30));
               // g.DrawImage(Image.FromFile("E:\\" + @"Pankaj/BarcodeDemo/BarcodeDemo/BarcodeImage/" + Filename), new Point(15, 15));
                //g.DrawImage(Image.FromFile("E:\\" + @"Pankaj/BarcodeDemo/BarcodeDemo/BarcodeImage/Sku.jpg"), new Point(25, 30));



                img.Save("E:\\" + @"Pankaj/BarcodeDemo/BarcodeDemo/BarcodeImage/"+Filename, ImageFormat.Jpeg);

                fileStream1.Close();
                fileStream2.Close();
                g.Dispose();

                 File.Delete("E:\\" + @"Pankaj/BarcodeDemo/BarcodeDemo/BarcodeImage/Sku.jpeg");
                 File.Delete("E:\\" + @"Pankaj/BarcodeDemo/BarcodeDemo/BarcodeImage/BarcodeImg.jpeg");

答案 2 :(得分:0)

Dim _strImagePath = ""
Dim _strImageName = "image.jpg"

'Get the path
_strImagePath = "YOUR_IMAGE_PATH"

'Delete the image physically
Dim strImageName As New FileInfo(_strImagePath & _strImageName)
If strImageName.Exists Then
   strImageName.Delete()
End If