无法使用位图。保存

时间:2018-07-13 08:13:20

标签: c# .net windows bitmap gdi+

我想制作一个屏幕捕捉器,每当用户单击它时,它就可以捕捉很多次(我想创建自己的屏幕捕捉器,所以不要只是告诉我有用于该操作的软件,而且是针对我自己的实践): )

Bitmap.Save "Generic Error"

我尝试从StackOverflow搜索所有可能的解决方案(上面的链接是我发现的众多解决方案之一),但是我无法解决我的问题。

这是我的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Screen_Monitoring
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCaptureScreen_Click(object sender, EventArgs e)
        {
            String screenName = AppDomain.CurrentDomain.BaseDirectory + "ScreenImages/screen.jpg";
            CaptureScreen(screenName);
            screen1.Image = Image.FromFile(screenName);
        }

        private void CaptureScreen(String screenName)
        {
            Rectangle bounds = Screen.PrimaryScreen.Bounds;
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }
                bitmap.Save(screenName, ImageFormat.Jpeg);
            }
        }
    }
}

当我尝试两次保存我的Picture Box(第二次总是发生)时,会出现问题

错误消息:System.Runtime.InteropServices.ExternalException:“ GDI +中发生了一般性错误。”

这是我的表单

Form Image

不要介意ScreenCapture.cs,这只是我尝试尝试“屏幕捕获”的其他解决方案之一。

编辑为重复 我刚刚尝试过Overwrite Existing Image,但仍然和以前一样错误。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Screen_Monitoring
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCaptureScreen_Click(object sender, EventArgs e)
        {
            String screenName = AppDomain.CurrentDomain.BaseDirectory + "ScreenImages/screen.jpg";
            if (File.Exists(screenName)) File.Delete(screenName);
            CaptureScreen(screenName);
            screen1.Image = Image.FromFile(screenName);
        }

        private Image GetCopyImage(string path)
        {
            using (Image im = Image.FromFile(path))
            {
                Bitmap bm = new Bitmap(im);
                return bm;
            }
        }

        private void CaptureScreen(String screenName)
        {
            Rectangle bounds = Screen.PrimaryScreen.Bounds;
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }
                bitmap.Save(screenName, ImageFormat.Jpeg);
                bitmap.Dispose();
            }
        }
    }
}

现在这是我得到错误的地方(再次,第二次单击后)

if (File.Exists(screenName)) File.Delete(screenName);

错误消息:System.IO.IOException:'该进程无法访问文件'C:\ Users \ Michael HT \ Documents \ 2。 Work \ Project \ Screen Monitoring \ Screen Monitoring \ bin \ Debug \ ScreenImages \ screen.jpg”,因为它正在被另一个进程使用。'

编辑-添加-空-删除前

private void btnCaptureScreen_Click(object sender, EventArgs e)
        {
            String screenName = AppDomain.CurrentDomain.BaseDirectory + "ScreenImages/screen.jpg";
            screen1.Image = null;
            if (File.Exists(screenName)) File.Delete(screenName);
            CaptureScreen(screenName);
            screen1.Image = Image.FromFile(screenName);
        }

我尝试在删除之前添加null,但仍然遇到相同的错误

if (File.Exists(screenName)) File.Delete(screenName);

错误消息:System.IO.IOException:'该进程无法访问文件'C:\ Users \ Michael HT \ Documents \ 2。 Work \ Project \ Screen Monitoring \ Screen Monitoring \ bin \ Debug \ ScreenImages \ screen.jpg”,因为它正在被另一个进程使用。'

解决方案

我尝试了这个,它成功了!

private void btnCaptureScreen_Click(object sender, EventArgs e)
        {
            String screenName = AppDomain.CurrentDomain.BaseDirectory + "ScreenImages/screen.jpg";
            if(screen1.Image != null) screen1.Image.Dispose();
            screen1.Image = null;
            CaptureScreen(screenName);
            screen1.Image = Image.FromFile(screenName);
        }

这是正确的代码:)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Screen_Monitoring
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCaptureScreen_Click(object sender, EventArgs e)
        {
            String screenName = AppDomain.CurrentDomain.BaseDirectory + "ScreenImages/screen.jpg";
            if(screen1.Image != null) screen1.Image.Dispose();
            screen1.Image = null;
            CaptureScreen(screenName);
            screen1.Image = Image.FromFile(screenName);
        }

        private Image GetCopyImage(string path)
        {
            using (Image im = Image.FromFile(path))
            {
                Bitmap bm = new Bitmap(im);
                return bm;
            }
        }

        private void CaptureScreen(String screenName)
        {
            Rectangle bounds = Screen.PrimaryScreen.Bounds;
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }
                bitmap.Save(screenName, ImageFormat.Jpeg);
            }
        }
    }
}

感谢您的帮助!

0 个答案:

没有答案