簡體   English   中英

如何將繪制的形狀另存為圖像C#

[英]How to save drawn shapes as images c#

我正在創建一個繪畫程序,我想出了如何在Form上繪制形狀,但是如何保存它們,我嘗試這樣做:

Image bmp = new Bitmap(this.Width - panel1.Width, this.Height - panel1.Height);
using (Graphics g = Graphics.FromImage(bmp))
{

}
bmp.Save("test.bmp"); 

它保存test.bmp但文件為空?

這是我的整個代碼:

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;
namespace Paint_Program
{

public partial class PaintImg : Form
{



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

    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 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 e)
    {
        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(e.X, e.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))
        {

        }
        bmp.Save("test.bmp");
    }

}
}

如果有人有新方法,我將不勝感激? :d

Bitmap bmp;
Graphics objGraphics;
Rectangle rt;
Point pnt;

rt = this.ClientRectangle;
pnt = this.PointToScreen(new Point(0, 0));

bmp = new Bitmap(rt.Width, rt.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

objGraphics = Graphics.FromImage(bmp);
objGraphics.CopyFromScreen(pnt.X, pnt.Y, 0, 0, rt.Size, CopyPixelOperation.SourceCopy);
objGraphics.Dispose();

bmp.Save("test.bmp");

bmp.Dispose();

瓦爾特

您發布的代碼沒有錯-它准確地繪制了您編寫的代碼-沒什么。 它會根據您指定的大小創建一個漂亮的空文件(根據您傳遞的參數,這可能是錯誤的)。

為了使某些內容顯示panel1_Click圖上,您需要進行繪制-即從panel1_Click重構代碼以進行繪制,或者至少對某些內容進行硬編碼:

Image bmp = new Bitmap(20, 20);
using (Graphics g = Graphics.FromImage(bmp))
{
   g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(3, 3, 8, 8));
}
bmp.Save("test.bmp");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM