借鉴面板,保存为位图位图、保存为、面板

2023-09-03 21:51:41 作者:╭ァ夨眠℅

我试图做一个简单的应用程序,用户可以绘制小组并将其保存到自己的电脑作为位图。当我继续保存的部分,但是,我得到的是一个空的(白色)位图。

我已经被浏览了许多其他解决方案,我是pretty的肯定,我现在的储蓄位图的正确方法,所以我开始怀疑我的拉丝工艺不正确。到底什么是错在这里?

 公共部分类Form1中:形态
{

    SolidBrush刷;
    喷喷;
    点[]点=新点[3]。
    图形显示;
    位图BMAP;

    公共Form1中()
    {
        的InitializeComponent();
        显示器= panel1.CreateGraphics();
        BMAP =新位图(panel1.Width,panel1.Height);
    }


    私人无效panel1_MouseDown(对象发件人,发送MouseEventArgs E)
    {
        刷=新SolidBrush(Color.Black);
        笔=新朋(Color.Black);


        display.FillEllipse(刷,e.X,e.Y,10,10);
        panel1.DrawToBitmap(BMAP,新矩形(0,0,panel1.Width,panel1.Height));

        //this.Invalidate();
    }



    私人无效clearToolStripMenuItem_Click(对象发件人,EventArgs的)
    {
        图形显示= panel1.CreateGraphics();
        display.Clear(panel1.BackColor);

    }

    私人无效saveToolStripMenuItem_Click(对象发件人,EventArgs的)
    {



        bmap.Save(@C:\ TEMP \ Test.bmp);

    }
}
 

编辑 有了这个版本,我只是得到一个黑色的BMP,我甚至不看到了我的屏幕上创建省略号。虽然我也注意到,如果我把无效,并绘制成位图早在MouseDown事件,然后保存按钮将保存最后椭圆形,而还有没有出现在我的屏幕上。

 私人无效panel1_MouseDown(对象发件人,发送MouseEventArgs E)
        {

            鼠标按下= TRUE;
            X = e.X;
            Y = e.Y;

        }

        私人无效panel1_Paint(对象发件人,PaintEventArgs的E)
        {
            //图形G = e.Graphics;
            如果(鼠标按下==真)

            {
            刷=新SolidBrush(Color.Black);
            笔=新朋(Color.Black);

            Graphics.FromImage(BMAP).FillEllipse(刷,X,Y,10,10);
            panel1.Invalidate();
           //panel1.DrawToBitmap(bmap,新的Rectangle(0,0,panel1.Width,panel1.Height));


            //panel1.Invalidate();

            }
        }
 

解决方案

由于汉斯做了大部分在他的评论的工作,这里是如何的code或许应该看看:

 公共部分Form1类:表格{
  位图BMAP;

  公共Form1中(){
    的InitializeComponent();

    BMAP =新位图(panel1.ClientWidth,panel1.ClientHeight);
    panel1.MouseDown + = panel1_MouseDown;
    panel1.Paint + = panel1_Paint;
  }

  无效panel1_Paint(对象发件人,PaintEventArgs的E){
    e.Graphics.DrawImage(BMAP,Point.Empty);
  }

  无效panel1_MouseDown(对象发件人,发送MouseEventArgs E){
    使用(图形G = Graphics.FromImage(BMAP)){
      g.FillEllipse(Brushes.Black,e.X,e.Y,10,10);
    }
    panel1.Invalidate();
  }

  私人无效clearToolStripMenuItem_Click(对象发件人,EventArgs的){
    使用(图形G = Graphics.FromImage(BMAP)){
      g.Clear(Color.White);
    }
    panel1.Invalidate();
  }

  私人无效saveToolStripMenuItem_Click(对象发件人,EventArgs的){
    bmap.Save(@C:\ TEMP \ bmap.bmp);
  }
}
 
如何在浏览器网页中显示word文件内容

的createGraphics只是一个暂时的画布,所以你很少,如果有的话,请使用绘图的目的,特别是因为你要保存的图像。

I'm trying to make a simple application where the user can draw on the Panel and save it to their computer as a bitmap. When I proceed to the save part, however, all I get is an empty (white) bitmap.

I've been browsing many other solutions and I am pretty sure I am saving the bitmap the correct way, so I am starting to wonder if my drawing process is incorrect. What exactly is wrong here?

public partial class Form1 : Form
{

    SolidBrush brush;
    Pen pen;
    Point[] points = new Point[3];
    Graphics display;
    Bitmap bmap;

    public Form1()
    {
        InitializeComponent();
        display = panel1.CreateGraphics();
        bmap = new Bitmap(panel1.Width, panel1.Height);
    }


    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        brush = new SolidBrush(Color.Black);
        pen = new Pen(Color.Black);


        display.FillEllipse(brush, e.X, e.Y, 10, 10);
        panel1.DrawToBitmap(bmap, new Rectangle(0, 0, panel1.Width, panel1.Height));

        //this.Invalidate();
    }



    private void clearToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Graphics display = panel1.CreateGraphics();
        display.Clear(panel1.BackColor);

    }

    private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {



        bmap.Save(@"C:\Temp\Test.bmp");

    }
}

EDIT With this revision, I just get a black bmp and I don't even see elipses being created anymore on my screen. Although I did notice that if I put invalidate and Draw to bitmap back in the mousedown event, then the save button will save the last ellipse, while there is still nothing appearing on my screen.

private void panel1_MouseDown(object sender, MouseEventArgs e)
        {

            mousedown = true;
            x = e.X;
            y = e.Y;

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            //Graphics g = e.Graphics;
            if(mousedown==true)

            {
            brush = new SolidBrush(Color.Black);
            pen = new Pen(Color.Black);

            Graphics.FromImage(bmap).FillEllipse(brush, x, y, 10, 10);
            panel1.Invalidate();
           //panel1.DrawToBitmap(bmap, new Rectangle(0, 0, panel1.Width, panel1.Height));


            //panel1.Invalidate();

            }
        }

解决方案

As Hans did most of the work in his comment, here is how your code should probably look:

public partial class Form1 : Form {
  Bitmap bmap;

  public Form1() {
    InitializeComponent();

    bmap = new Bitmap(panel1.ClientWidth, panel1.ClientHeight);
    panel1.MouseDown += panel1_MouseDown;
    panel1.Paint += panel1_Paint;
  }

  void panel1_Paint(object sender, PaintEventArgs e) {
    e.Graphics.DrawImage(bmap, Point.Empty);
  }

  void panel1_MouseDown(object sender, MouseEventArgs e) {
    using (Graphics g = Graphics.FromImage(bmap)) {
      g.FillEllipse(Brushes.Black, e.X, e.Y, 10, 10);
    }
    panel1.Invalidate();
  }

  private void clearToolStripMenuItem_Click(object sender, EventArgs e) {
    using (Graphics g = Graphics.FromImage(bmap)) {
      g.Clear(Color.White);
    }
    panel1.Invalidate();
  }

  private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
    bmap.Save(@"c:\temp\bmap.bmp");
  }
}

CreateGraphics is just a temporary canvas, so you rarely, if ever, use that for drawing purposes, especially since you are trying to save an image.