【问题标题】:Why when cropping image i'm getting black image of the cropped image?为什么在裁剪图像时我得到裁剪图像的黑色图像?
【发布时间】:2017-07-27 21:44:05
【问题描述】:

在form1构造函数中

public Form1()
        {
            InitializeComponent();

            BackColor = Color.LightGreen;
            TransparencyKey = Color.LightGreen;
            this.TopMost = true;
            this.Location = new Point(0, 0);
            timer1.Enabled = true;
        }

然后:

private void Cap()
        {
            countImages++;            
            ScreenCapture sc = new ScreenCapture();            
            Image img = sc.CaptureScreen();
            Bitmap bmp = new Bitmap(img);
            Bitmap source = new Bitmap(this.Width, this.Height);
            Rectangle section = new Rectangle(new Point(source.Width, source.Height), source.Size);
            Bitmap CroppedImage = CropImage(source, section);
            CroppedImage.Save("c:\\temp\\screens\\" + countImages + ".gif", ImageFormat.Gif);
            source.Dispose();  
        }

public Bitmap CropImage(Bitmap source, Rectangle section)
        {
            // An empty bitmap which will hold the cropped image
            Bitmap bmp = new Bitmap(section.Width, section.Height);

            Graphics g = Graphics.FromImage(bmp);

            // Draw the given area (section) of the source image
            // at location 0,0 on the empty bitmap (bmp)
            g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);

            return bmp;
        }

我保存到硬盘的位图:

CroppedImage.Save("c:\\temp\\screens\\" + countImages + ".gif", ImageFormat.Gif);

他们都是黑色的。 我想从图像中裁剪这部分并将其保存到硬盘中。

屏幕截图示例以及我想从中裁剪的内容: 我用红色圆圈标记了透明的 form1,这就是我想要裁剪 form1 的部分并将其保存为 form1 部分的裁剪图像:

我首先尝试了这个:

private void CaptureDesktop()
        {
            countImages++;
            ScreenCapture sc = new ScreenCapture();                    
            Image img = sc.CaptureScreen();
            Bitmap bmp = new Bitmap(img);            
            Bitmap source = new Bitmap(this.Width, this.Height);
            Rectangle section = new Rectangle(new Point(source.Width, source.Height), source.Size);
            Bitmap CroppedImage = CropImage(bmp, section);
            CroppedImage.Save("c:\\temp\\desktopscreens\\" + countImages + ".gif", ImageFormat.Gif);
        }

但是我得到了这个裁剪的图像结果:奇怪的画中画?

然后我尝试了这段代码:

using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                        Screen.PrimaryScreen.Bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bmpScreenCapture))
                {

                    g.CopyFromScreen(this.Location.X,
                                   this.Location.Y,
                                   0, 0,
                                   this.Size,
                                   CopyPixelOperation.SourceCopy);


                }

                Rectangle section = new Rectangle(new Point(this.Location.X, this.Location.Y), new Size(this.Width, this.Height));
                Bitmap CroppedImage = CropImage(bmpScreenCapture, section);
                CroppedImage.Save("c:\\temp\\desktopscreens\\1.bmp", ImageFormat.Bmp);


            }

但结果是:

不是我想要的。我只想从整个图像中裁剪 form1 部分。

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    您执行Bitmap source = new Bitmap(this.Width, this.Height); 这将创建一个黑色图像。

    然后你继续裁剪它。那仍然是黑色图像。

    然后您继续保存它。那仍然是黑色图像。

    也许您的意思是裁剪 Bitmap bmpImage img,而不是 Bitmap source。我不知道Bitmap source 是什么意思。

    【讨论】:

    • 当我在构造函数中使 form1 透明时: TransparencyKey = Color.LightGreen;然后当我截取整个桌面的屏幕截图时: Image img = sc.CaptureScreen();我现在想从桌面的 img 中只裁剪透明 form1 的一部分。
    • 我用截图示例编辑了我的问题,并用红色圆圈标记了我要裁剪的部分。位图源应该是 form1 部分。至少这是我的意思。所以我知道它是黑色的,但我怎样才能从 bmp 只裁剪 form1 的一部分?
    • 这是一个不同的问题。你的问题是“我为什么变黑”,这就是我的回答。
    • 所以,发布一个不同的问题。或者只是谷歌它,那里有很多示例源代码,这里是 stackoverflow。
    【解决方案2】:

    试试这个(来自Capture the screen shot using .NET的一些代码)

     private void Cap()
        {
            using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                            Screen.PrimaryScreen.Bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bmpScreenCapture))
                {
    
                    g.CopyFromScreen(this.Location.X,
                                   this.Location.Y,
                                   0, 0,
                                   this.Size,
                                   CopyPixelOperation.SourceCopy);
    
    
                }
    
                Rectangle section = new Rectangle(new Point(this.Location.X, this.Location.Y), new Size(this.Width, this.Height));
                Bitmap CroppedImage = CropImage(bmpScreenCapture, section);
                CroppedImage.Save("c:\\temp\\screens\1.bmp", ImageFormat.Bmp);
    
    
            }
    
    
    
    
        }
    
        public Bitmap CropImage(Bitmap source, Rectangle section)
        {
            // An empty bitmap which will hold the cropped image
            Bitmap bmp = new Bitmap(section.Width, section.Height);
    
            Graphics g = Graphics.FromImage(bmp);
    
            // Draw the given area (section) of the source image
            // at location 0,0 on the empty bitmap (bmp)
            g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
    
            return bmp;
        }
    

    【讨论】:

    • 即使该行是第一个 CroppedImage.Save("c:\\temp\\screens\\1.bmp", ImageFormat.Bmp);同样的例外。
    • 可能是权限。您可以尝试将其保存到另一个文件夹吗?
    • 我尝试了你的最后一个解决方案,并用我得到的内容更新了我的问题。这不是我想要的。对不起。
    • 使用我一起发布的所有代码似乎给了我正确的结果。它基本上捕获了 Form1 区域中包含的所有内容。见图片。
    猜你喜欢
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 2023-04-02
    • 2015-02-02
    • 1970-01-01
    相关资源
    最近更新 更多