Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不允许删除JPEG(由其他进程使用)_C#_Jpeg - Fatal编程技术网

C# 不允许删除JPEG(由其他进程使用)

C# 不允许删除JPEG(由其他进程使用),c#,jpeg,C#,Jpeg,问题编辑!!这是一个可编译文件的示例,可以帮助您找到我的问题! 我将一些屏幕截图保存在特定的文件中。 当我使用函数DeleteFile()时,它应该先删除内容,然后删除文件夹,但是我得到“另一个进程正在使用该icon.jpg”! (无论哪种方式删除文件夹,都必须没有内容!) ImageExtensions.cs 截屏 AnotherFile.cs(使用PrintScreen()) 表格1.cs(主要) 使用。。。 使用。。。 名称空间名称{ 公共部分类Form1:Form { 常量int Max

问题编辑!!这是一个可编译文件的示例,可以帮助您找到我的问题! 我将一些屏幕截图保存在特定的文件中。 当我使用函数DeleteFile()时,它应该先删除内容,然后删除文件夹,但是我得到“另一个进程正在使用该icon.jpg”! (无论哪种方式删除文件夹,都必须没有内容!)

ImageExtensions.cs

截屏

AnotherFile.cs(使用PrintScreen())

表格1.cs(主要)

使用。。。
使用。。。
名称空间名称{
公共部分类Form1:Form
{
常量int MaxRetries=3;
const int DelayBetweenRetries=1000;
const int ERROR\u SHARING\u违例=未检查((int)0x80070020);
string file_images=_images_文件的我的_路径_;
公共表格1()
{
初始化组件();
CreateFile();
}
私有void Form1_Load(对象发送方,事件参数e){}
public void CreateFile()
{
如果(!Directory.Exists(文件\图像))
{
DirectoryInfo di=Directory.CreateDirectory(文件\图像);
}
}
公共void DeleteFiles()
{
字符串[]filestodelite=Directory.GetFiles(文件\图像);
if(filesToDelete!=null)
{
var files=filesToDelete.Where(x=>Path.GetExtension(x).Contains(“.jpg”);
foreach(文件中的var文件)
{
var temp=文件;
DeleteFile(temp);//删除JPEG
} 
}
Directory.Delete(file_images);//删除文件夹
}
公共静态void DeleteFile(字符串路径)
{
对于(int i=0;i
我以前遇到过这个问题,我通过将图像数据从文件流复制到memorystream中,然后从中加载图像来解决它。和保存图像类似。这样,即使图像保留对源或保存流的引用,它也不是物理文件

更完整的解决方案:

尝试:


我是内联编写的,所以如果它不能正常工作,请对其进行调整。

GDI+会一直使用该文件,直到图像被正确处理……我使用了image.Dispose();但我有同样的问题!这是插入此内容的正确行吗?请尝试使用(var图像…)。您遇到的问题几乎总是因为某个地方有一个未处理的图像对象而导致的。顺便说一句,JPEG通常不是用于屏幕截图之类的东西的好文件格式(包括文本和实体边框的东西—JPEG处理得很差)。使用PNG或类似的东西。@Phylogenesis谢谢你的提示!如果我和你的助手一起创建了一个新的类文件,那么我应该使用什么命令?(我不太擅长编程,所以有点混乱),如果你有时间,我会很感激的!我的课程是关于加载文件的。对于您的情况(保存),请尝试使用内存流。我这样编辑了PrintScreen()函数,但仍然出现错误
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace name
{
    static class ImageExtensions
    {
        public static void SaveToFile(Image image,string path,ImageFormat format)
        {
            using (var stream = File.OpenWrite(path))
            {
                image.Save(stream, format);
            }

        }
    }
}
using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace name
{
    public class ScreenCapture
    {

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);

        [StructLayout(LayoutKind.Sequential)]
        private struct Rect
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetDesktopWindow();


        public static Image CaptureDesktop()
        {
            return CaptureWindow(GetDesktopWindow());
        }

        public static Bitmap CaptureActiveWindow()
        {
            return CaptureWindow(GetForegroundWindow());
        }


        public static Bitmap CaptureWindow(IntPtr handle)
        {
            var rect = new Rect();
            GetWindowRect(handle, ref rect);
            var bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
            var result = new Bitmap(bounds.Width, bounds.Height);

            using (var graphics = Graphics.FromImage(result))
            {
               graphics.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
            }

            return result;
        }
    }
}
public void PrintScreen()
{
    using (var image = ScreenCapture.CaptureDesktop())
    {
        ImageExtensions.SaveToFile(image, (file_images + ".jpg"), ImageFormat.Jpeg);
    }
}
using ...
using ...

namespace name{

    public partial class Form1 : Form
    {
    const int MaxRetries = 3;
    const int DelayBetweenRetries = 1000;
    const int ERROR_SHARING_VIOLATION = unchecked((int)0x80070020);
    string file_images = my_path_of_images_file;

    Public Form1()
    {
        InitializeComponent();
        CreateFile();
    }

    private void Form1_Load(object sender, EventArgs e){}

    public void CreateFile()
    {
        if (!Directory.Exists(file_images))
        {
            DirectoryInfo di = Directory.CreateDirectory(file_images);
        }
    }


    public void DeleteFiles()
    {
         string[] filesToDelete = Directory.GetFiles(file_images);
         if (filesToDelete != null)
         {
            var files = filesToDelete.Where(x => Path.GetExtension(x).Contains(".jpg"));
                foreach (var file in files)
                {
                   var temp = file;
                   DeleteFile(temp); // Delete JPEG
                } 
            }
        Directory.Delete(file_images); // Delete Folder
    }


    public static void DeleteFile(string path)
    {
        for (int i = 0; i < MaxRetries; ++i)
        {
            try
            {
                File.Delete(path);
            }
            catch (IOException e)
            {
                // You may also sleep whatever the error is...
                if (Marshal.GetHRForException(e) == ERROR_SHARING_VIOLATION)
                {
                    Thread.Sleep(DelayBetweenRetries);
                    continue;
                }
                throw;
             }
          }
      }
    } // End Of Main.cs
} // End Of Namespace
using (var ms=new MemoryStream()) {
  image.Save(ms,format);
  File.WriteAllBytes(path,ms.ToArray());
}