为什么当我创建位图时,它全是空的?

罗南·瓜塔(Ronen Gueta)
private Bitmap CropImage()
        {
            Bitmap pic = pictureBoxSnap.Image as Bitmap;
            Bitmap cropped = new Bitmap(rect.Width, rect.Height);

            using (Graphics g = Graphics.FromImage(cropped))
            {
                g.DrawImage(pic, new Rectangle(0, 0, rect.Width, rect.Height),
                             rect, GraphicsUnit.Pixel);
            }
            pic.Save(@"c:\temp\testingitimage.jpg");
            cropped.Save(@"c:\temp\testingitimage1.bmp");
            return cropped;

        }

我添加了这两行保存来测试我得到了什么。我看到的第一个图片在pictureBox中是正确的图像。但是第二个作物全是白色和空的。

我从pictureBox绘画事件中调用此方法:

private void pictureBoxSnap_Paint(object sender, PaintEventArgs e)
        {

            if (pictureBoxSnap.Image != null)
            {
                {
                    if (ClearGraphics == false)
                    {
                        if (cropRect == false)
                        {
                            if (rectangles[listBoxSnap.SelectedIndex] != Rectangle.Empty)
                            {
                                e.Graphics.DrawRectangle(Pens.Firebrick, rectangles[listBoxSnap.SelectedIndex]);
                            }
                        }
                        else
                        {
                            if (rect.Width > 10 && rect.Height > 10)
                            {
                                e.Graphics.DrawRectangle(Pens.Gray, rect);
                                pictureBoxSnap.Image = CropImage();
                                Image img = CropImage();
                                img.Save(@"c:\temp\testimageing.jpg");
                            }
                        }
                    }
                }
            }           
        }

我在以下位置使用了一个断点:e.Graphics.DrawRectangle(Pens.Gray,rect); 并且rect不为空(rect是Rectangle,我在pictureBox上绘制,然后当我单击按钮时,它对pict​​ureBox进行验证)。

我也在这里进行了保存并进行了testimageing,我在硬盘上看到它是黑色的,但是当我用Paint编辑它时,我看到它是白色的。

例如,当我在pictureBox上绘制一个矩形时,我在CropImage方法中使用了一个断点,我看到变量rect例如:X = 136 Y = 149 Width = 131 Height = 106在绘制事件中,这个rec​​t变量也具有相同的值。

并且pictureBox的大小为:640x480并且pictureBox属性的SizeMode设置为:zoom

我为什么得到空的白色图像?而不是其中部分图像位于内部的裁剪矩形吗?

编辑:

这是我现在使用的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DannyGeneral;
using System.Diagnostics;

namespace MinimizeCapture
{
    public partial class Form1 : Form
    {
        Boolean bHaveMouse;
        Point ptOriginal = new Point();
        Point ptLast = new Point();
        bool cropRect;
        Bitmap cloneBitmap;
        Rectangle recttest;
        private Rectangle Rect;
        private Rectangle[] rectangles;
        private Rectangle RectClone;
        private bool btn = false;
        private Point RectStartPoint = Point.Empty;
        private Point RectEndPoint = Point.Empty;
        private Brush selectionBrush = new SolidBrush(Color.Red);
        private Pen pen;
        private string selectedIndex;
        private List<string> drawnItems = new List<string>();
        private bool ClearGraphics;

        public Form1()
        {
            InitializeComponent();

            cropRect = false;
            var windows = OpenWindowGetter.FindWindowsWithText();
            ClearGraphics = false;
            this.DoubleBuffered = true;
            btn = false;
            pen = new Pen(selectionBrush);
            buttonSnap.Enabled = false;
            backgroundWorker1.RunWorkerAsync();
        }

        private void buttonSnap_Click(object sender, EventArgs e)
        {
            ClearGraphics = true;
            this.listBoxSnap.Items.Clear();
            this.pictureBoxSnap.Image = null;
            backgroundWorker1.RunWorkerAsync();
        }

        private void listBoxSnap_SelectedIndexChanged(object sender, EventArgs e)
        {

            WindowSnap snap = this.listBoxSnap.SelectedItem as WindowSnap;
            selectedIndex = this.listBoxSnap.SelectedIndex.ToString();
            this.pictureBoxSnap.Image = snap.Image;
            for (int i = 0; i < rectangles.Length; i++)
            {
                if (rectangles[i] != RectClone)
                {
                    ClearGraphics = false;
                }
                else
                {
                    ClearGraphics = true;
                }
            }

        }

        private void checkBoxForceMDI_CheckedChanged(object sender, EventArgs e)
        {
            WindowSnap.ForceMDICapturing = (sender as CheckBox).Checked;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            listBoxSnap.Invoke(new MethodInvoker(delegate { this.listBoxSnap.Items.Add("Minimized Windows"); }));
            listBoxSnap.Invoke(new MethodInvoker(delegate { this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true,true).ToArray()); }));
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            rectangles = new Rectangle[listBoxSnap.Items.Count];
            buttonSnap.Enabled = true;
        }

        private void pictureBoxSnap_Paint(object sender, PaintEventArgs e)
        {

            if (pictureBoxSnap.Image != null)
            {
                {
                    if (ClearGraphics == false)
                    {

                            if (rectangles[listBoxSnap.SelectedIndex] != Rectangle.Empty)
                            {
                                e.Graphics.DrawRectangle(Pens.Firebrick, rectangles[listBoxSnap.SelectedIndex]);
                            }




                    }
                    if (cropRect == true)
                    {
                        if (recttest.Width > 10 && recttest.Height > 10)
                        {
                            //e.Graphics.DrawRectangle(Pens.Gray, recttest);
                            e.Graphics.Clear(Color.White);
                            /*pictureBoxSnap.Image = CropImage();
                            Image img = CropImage();
                            img.Save(@"c:\temp\testimageing.jpg");*/

                        }
                    }
                }
            }           
        }

        private void pictureBoxSnap_MouseMove(object sender, MouseEventArgs e)
        {

            if (btn == true)
            {
                ClearGraphics = false;
                RectEndPoint = e.Location;
                int currentindex = listBoxSnap.SelectedIndex;
                rectangles[currentindex] = RectClone;
                Rect = getRect(RectStartPoint, RectEndPoint);
                RectClone = Rect;
                pictureBoxSnap.Invalidate();
            }

            Point ptCurrent = new Point(e.X, e.Y);
        // If we "have the mouse", then we draw our lines.
            if (bHaveMouse)
            {
                // If we have drawn previously, draw again in
                // that spot to remove the lines.
                if (ptLast.X != -1)
                {
                    MyDrawReversibleRectangle(ptOriginal, ptLast);
                }
                // Update last point.
                ptLast = ptCurrent;
                // Draw new lines.
                MyDrawReversibleRectangle(ptOriginal, ptCurrent);
            }
        }

        private void pictureBoxSnap_MouseDown(object sender, MouseEventArgs e)
        {
            RectStartPoint = e.Location;
            btn = true;
            Rect = Rectangle.Empty;
            RectClone = Rectangle.Empty;

            bHaveMouse = true;
            // Store the "starting point" for this rubber-band rectangle.
            ptOriginal.X = e.X;
            ptOriginal.Y = e.Y;
            // Special value lets us know that no previous
            // rectangle needs to be erased.
            ptLast.X = -1;
            ptLast.Y = -1;
        }

        private void pictureBoxSnap_MouseUp(object sender, MouseEventArgs e)
        {
            recttest = rectangles[listBoxSnap.SelectedIndex];
            ClearGraphics = false;
            btn = false;
            RectEndPoint = e.Location;
            pictureBoxSnap.Invalidate();
            int currentindex = listBoxSnap.SelectedIndex;
            rectangles[currentindex] = RectClone;

            // Set internal flag to know we no longer "have the mouse".
            bHaveMouse = false;
            // If we have drawn previously, draw again in that spot
            // to remove the lines.
            if (ptLast.X != -1)
            {
                Point ptCurrent = new Point(e.X, e.Y);
                MyDrawReversibleRectangle(ptOriginal, ptLast);
            }
            // Set flags to know that there is no "previous" line to reverse.
            ptLast.X = -1;
            ptLast.Y = -1;
            ptOriginal.X = -1;
            ptOriginal.Y = -1;
            pictureBoxSnap.Invalidate();
        }

        Rectangle getRect(Point p1, Point p2)
        {
            Point p = new Point(Math.Min(p1.X, p2.X), Math.Min(p1.Y, p2.Y));
            Size s = new Size(Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y));
            return new Rectangle(p, s);
        }

        private void ConfirmRectangle_Click(object sender, EventArgs e)
        {
            ConfirmRectangle.ForeColor = Color.Red;
            ConfirmRectangle.Enabled = false;
            StreamWriter w = new StreamWriter(@"c:\temp\Settings.txt", true);
            w.WriteLine("Rectangle Location: " + RectClone.Location + " Rectangle Size: " + RectClone.Size + " Selected Index: " + selectedIndex);
            textBoxIndex.Text = selectedIndex.ToString();
            w.Close();
            cropRect = false;
            //pictureBoxSnap.Invalidate();
            Image img = CropImage();
            img.Save(@"c:\temp\testimageing.jpg");
            pictureBoxSnap.Image = img;

        }

        // Convert and normalize the points and draw the reversible frame.
        private void MyDrawReversibleRectangle(Point p1, Point p2)
        {
            Rectangle rc = new Rectangle();
            // Convert the points to screen coordinates.
            p1 = pictureBoxSnap.PointToScreen(p1);
            p2 = pictureBoxSnap.PointToScreen(p2);
            // Normalize the rectangle.
            if (p1.X < p2.X)
            {
                rc.X = p1.X;
                rc.Width = p2.X - p1.X;
            }
            else
            {
                rc.X = p2.X;
                rc.Width = p1.X - p2.X;
            }
            if (p1.Y < p2.Y)
            {
                rc.Y = p1.Y;
                rc.Height = p2.Y - p1.Y;
            }
            else
            {
                rc.Y = p2.Y;
                rc.Height = p1.Y - p2.Y;
            }
            // Draw the reversible frame.
            rect = new Rectangle(pictureBoxSnap.PointToClient(rc.Location), rc.Size);
            ControlPaint.DrawReversibleFrame(rc, Color.Gray, FrameStyle.Dashed);
        }

        Rectangle rect = Rectangle.Empty;

        private Bitmap CropImage()
        {
            //rect = recttest;
            Bitmap pic = pictureBoxSnap.Image as Bitmap;
            Bitmap cropped = new Bitmap(rect.Width, rect.Height);

            using (Graphics g = Graphics.FromImage(cropped))
            {
                g.DrawImage(pic, new Rectangle(0, 0, rect.Width, rect.Height),
                             rect, GraphicsUnit.Pixel);
            }
            pic.Save(@"c:\temp\testingitimage.jpg");
            cropped.Save(@"c:\temp\testingitimage1.bmp");
            return cropped;

        }
    }
}

将CropImage调用方法从绘画中移出,并将其添加到按钮上,单击此处:

private void ConfirmRectangle_Click(object sender, EventArgs e)
        {
            ConfirmRectangle.ForeColor = Color.Red;
            ConfirmRectangle.Enabled = false;
            StreamWriter w = new StreamWriter(@"c:\temp\Settings.txt", true);
            w.WriteLine("Rectangle Location: " + RectClone.Location + " Rectangle Size: " + RectClone.Size + " Selected Index: " + selectedIndex);
            textBoxIndex.Text = selectedIndex.ToString();
            w.Close();
            cropRect = false;
            //pictureBoxSnap.Invalidate();
            Image img = CropImage();
            img.Save(@"c:\temp\testimageing.jpg");
            pictureBoxSnap.Image = img;

        }

这行:

Image img = CropImage();
img.Save(@"c:\temp\testimageing.jpg");
pictureBoxSnap.Image = img;

但是我在pictureBox中得到的是这样的:

eZoom in?

不知道为什么。矩形位于我绘制的位置,但是可以显示原始图像在矩形中剪切的部分,就像放大一样。

γηράσκωδ'αείπολλάδιδασκόμε
private Bitmap CropImage(){
    Bitmap pic = pictureBoxSnap.Image as Bitmap;
    Bitmap cropped = new Bitmap(rect.Width, rect.Height);

    using (Graphics g = Graphics.FromImage(cropped)){
        g.DrawImage(pic, new Rectangle(0, 0, rect.Width, rect.Height),
                         rect, GraphicsUnit.Pixel);
    }

    using (Graphics g = Graphics.FromImage(pic)){
        g.DrawImage(cropped , 136, 149, rect.Width, rect.Height);
        //Draw the rectangle
    }

    //pic.Save(@"c:\temp\testingitimage.jpg");
    //cropped.Save(@"c:\temp\testingitimage1.bmp");
    return pic;
}

瓦尔特

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么当我创建这个新的位图时,它的背景是深灰色?如何将其设置为布局背景的相同颜色?

来自分类Dev

为什么当我不碰它时对象会改变?

来自分类Dev

当我使用maven-release-plugin释放分支时,为什么它尝试从修订版0创建分支?

来自分类Dev

当我尝试将WebView保存为PDF时,为什么NSPrintOperation创建一个空的PDF?

来自分类Dev

为什么当我尝试从父类创建子类(向下转换)时,得到一个空对象

来自分类Dev

当我尝试将WebView保存为PDF时,为什么NSPrintOperation创建一个空的PDF?

来自分类Dev

为什么当我运行它时不显示我的画布?

来自分类Dev

PHP YII 2为什么当我添加新页面时,它什么也不显示?

来自分类Dev

为什么当我在sale.py中创建一个字段时,却未在Postgresql数据库中创建它?

来自分类Dev

当我包含扩展小部件时,为什么没有显示?它只是不显示(例如,当我用容器替换它时,它显示)

来自分类Dev

当我告诉AngularJS $ scope.watch()观看数组时,为什么它停止工作?

来自分类Dev

当我单击选项卡时,为什么它没有打开?

来自分类Dev

当我将LinearLayout限制在父级的底部时,为什么它显示在顶部?

来自分类Dev

为什么当我从Firebase获取数据时,它返回NULL?

来自分类Dev

当我们想要删除它时,为什么要在集合名称后添加“s”?

来自分类Dev

当我添加到它时,PHP 数组不断被覆盖......为什么?

来自分类Dev

PHP - 当我可以通过 $_File 数组获取它时为什么要使用 pathinfo

来自分类Dev

为什么当我输入这个 powershell 代码时它没有被执行?

来自分类Dev

当我使用多个“if”循环时,为什么它一直说“语法错误”?

来自分类Dev

当我尝试打印封面数组时,为什么我的输出为空?

来自分类Dev

当我使用 $_POST['area'] = $_SESSION['area'] 时,为什么会这样,为什么它的长度会改变?

来自分类Dev

为什么缩放后我的位图似乎是空的

来自分类Dev

为什么我得到一个空的位图?

来自分类Dev

为什么当我创建一个新活动时,“ setContentView”无法识别我的布局?

来自分类Dev

当我在此GroovyShell中创建新类时,为什么什么也没发生?

来自分类Dev

当我在Goroutine中填充此地图时,为什么该地图为空?

来自分类Dev

当我尝试处理溢出菜单上的空值时,为什么会出现此异常?

来自分类Dev

当我尝试在 Room 中插入元素时,为什么会出现“空对象引用”异常?

来自分类Dev

当我尝试在 Room 中插入元素时,为什么会出现“空对象引用”异常?

Related 相关文章

  1. 1

    为什么当我创建这个新的位图时,它的背景是深灰色?如何将其设置为布局背景的相同颜色?

  2. 2

    为什么当我不碰它时对象会改变?

  3. 3

    当我使用maven-release-plugin释放分支时,为什么它尝试从修订版0创建分支?

  4. 4

    当我尝试将WebView保存为PDF时,为什么NSPrintOperation创建一个空的PDF?

  5. 5

    为什么当我尝试从父类创建子类(向下转换)时,得到一个空对象

  6. 6

    当我尝试将WebView保存为PDF时,为什么NSPrintOperation创建一个空的PDF?

  7. 7

    为什么当我运行它时不显示我的画布?

  8. 8

    PHP YII 2为什么当我添加新页面时,它什么也不显示?

  9. 9

    为什么当我在sale.py中创建一个字段时,却未在Postgresql数据库中创建它?

  10. 10

    当我包含扩展小部件时,为什么没有显示?它只是不显示(例如,当我用容器替换它时,它显示)

  11. 11

    当我告诉AngularJS $ scope.watch()观看数组时,为什么它停止工作?

  12. 12

    当我单击选项卡时,为什么它没有打开?

  13. 13

    当我将LinearLayout限制在父级的底部时,为什么它显示在顶部?

  14. 14

    为什么当我从Firebase获取数据时,它返回NULL?

  15. 15

    当我们想要删除它时,为什么要在集合名称后添加“s”?

  16. 16

    当我添加到它时,PHP 数组不断被覆盖......为什么?

  17. 17

    PHP - 当我可以通过 $_File 数组获取它时为什么要使用 pathinfo

  18. 18

    为什么当我输入这个 powershell 代码时它没有被执行?

  19. 19

    当我使用多个“if”循环时,为什么它一直说“语法错误”?

  20. 20

    当我尝试打印封面数组时,为什么我的输出为空?

  21. 21

    当我使用 $_POST['area'] = $_SESSION['area'] 时,为什么会这样,为什么它的长度会改变?

  22. 22

    为什么缩放后我的位图似乎是空的

  23. 23

    为什么我得到一个空的位图?

  24. 24

    为什么当我创建一个新活动时,“ setContentView”无法识别我的布局?

  25. 25

    当我在此GroovyShell中创建新类时,为什么什么也没发生?

  26. 26

    当我在Goroutine中填充此地图时,为什么该地图为空?

  27. 27

    当我尝试处理溢出菜单上的空值时,为什么会出现此异常?

  28. 28

    当我尝试在 Room 中插入元素时,为什么会出现“空对象引用”异常?

  29. 29

    当我尝试在 Room 中插入元素时,为什么会出现“空对象引用”异常?

热门标签

归档