繁体   English   中英

将绘图图形发布到C#中的位图

[英]Issue drawing graphics to Bitmap in c#

我正在尝试制作绘画程序。 我已经掌握了基本功能,并且记录了所有鼠标单击,因此我可以将它们绘制到位图上,然后保存它,除非它不起作用,使用这种方法,我可以重绘图形,除非尝试保存该文件位图图像为空白?

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel; 
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace Paint_Program
{
    public partial class PaintImg : Form
    {

    public Boolean veryimportantbool = false;
    int drawcount = 0;
    int brushsize = 16;

    List<Point> pts = new List<Point>();
    Point recordpoint { get; set; }

    public PaintImg()
    {
        InitializeComponent();
        //Cursor drawCursor = new Cursor("Pencil.cur");
        //this.Cursor = drawCursor;


    }


    private void Form1_Load(object sender, EventArgs e)
    {


        //Cursor.Position = new Point(this.Location.X - this.Width / 2, this.Location.Y - this.Height / 2)
        UserControl1 userc1 = new UserControl1();
        userc1.Show();
        brushsize = Convert.ToInt16(label1.Text);


    }

    private void rectangleShape1_Click(object sender, EventArgs e)
    {

    }

    private void Test_Click(object sender, EventArgs e)
    {
        //testing 
        //Console.WriteLine("X:" + mousex);
        //onsole.WriteLine("Y: " + mousey);
    }

    private void Form1_SizeChanged(object sender, EventArgs e)
    {

    }


    public void Form1_MouseClick(object sender, MouseEventArgs i)
    {

            drawcount = drawcount + 1;
            System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
            System.Drawing.Graphics formGraphics;
            formGraphics = this.CreateGraphics();
            formGraphics.FillRectangle(myBrush, new Rectangle(i.X, i.Y, brushsize, brushsize));
            myBrush.Dispose();
            formGraphics.Dispose();
            count.Text = Convert.ToString(drawcount);


    }

    private void Paint_Paint(object sender, PaintEventArgs e)
    {
        //this method will be used later when page resizing is added to the program.

        //Graphics g = e.Graphics;
        //Control control = (Control)sender;
        //drawPoint = control.PointToScreen(new Point(MousePosition.X, MousePosition.Y));
        //g.DrawRectangle(System.Drawing.Pens.Red, drawPoint.X, drawPoint.Y, 1, 1);

    }

    private void newToolStripButton_Click(object sender, EventArgs e)
    {
        //SetupDoc newsetup = new SetupDoc();
        //newsetup.ShowDialog();
    }

    private void PaintImg_MouseMove(object sender, MouseEventArgs e)
    {

    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {

    }

    private void panel1_Click(object sender, MouseEventArgs i)
    {
        drawcount = drawcount + 1;
        recordpoint = new Point(i.X, i.Y);
        pts.Add(recordpoint);
        System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
        System.Drawing.Graphics formGraphics;
        formGraphics = this.CreateGraphics();
        formGraphics.FillRectangle(myBrush, new Rectangle(i.X, i.Y, brushsize, brushsize));
        myBrush.Dispose();
        formGraphics.Dispose();
        count.Text = Convert.ToString(drawcount);
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        brushsize = brushsize + 1;
        label1.Text = Convert.ToString(brushsize);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        brushsize = brushsize - 1;
        label1.Text = Convert.ToString(brushsize);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Image bmp = new Bitmap(this.Width - panel1.Width, this.Height - panel1.Height);
        using (Graphics g = Graphics.FromImage(bmp))
        {
            foreach (var recordpoint in pts)
            {

                g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(Convert.ToInt16(recordpoint.X), Convert.ToInt16(recordpoint.Y), brushsize, brushsize));
            }
        }
        bmp.Save("testing.bmp");
    }

}
}

如果有人知道我在做什么错,请告诉我!

panel1_Click在您的pts集合中记录坐标,但是Form1_MouseClick事件不会执行此操作。 不过,两种方法都可以得出要点。

您确定要添加到期望的pts集合中吗?

检查位图大小

this.Width - panel1.Width,this.Height - panel1.Height

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM