【发布时间】:2018-08-03 20:45:51
【问题描述】:
我正在使用 C#,因此我启动了一个“Windows 窗体应用程序”以使用“图片框”,我可以在其中绘制一些东西。我想知道是否有办法将文件保存在某种文件(如 JPG)中。下面是一个示例,我在图片框中绘制了一条简单的线,高度为 300,宽度为 300。
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 AskStack
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Here I declare 'MyDraw' and a Pen that I'm gonna use
Graphics MyDraw;
Pen BluePen = new Pen(Color.Blue, 10);
// What happens when I click in my button
private void ButtonDraw_Click(object sender, EventArgs e)
{
MyDraw.Clear(Color.White);
// Here I relate MyDraw to the pictureBox that I have in my Form
MyDraw = pictureBox1.CreateGraphics();
// Here I Just draw a simple line
MyDraw.DrawLine(BluePen, 25, 25, 80, 80);
// I don't know exactly what this does
MyDraw.Save();
}
}
}
我想知道是否有办法将此图像保存在我可以轻松访问的文件中。另外,我想知道是否有一种方法可以在不使用图片框的情况下保存 JPG 文件并确定文件的大小。谢谢!!!
【问题讨论】:
-
你应该仔细阅读这里的很多很多关于绘图的帖子。不要存储图形对象或 Pens,不要使用 CreateGraphics 并保存它,您要绘制到位图。并始终处理您创建的图形对象
-
Plutonix 是对的。您的代码在很多方面都存在错误。一是你画的东西不能保存,只能截图。 ((顺便说一句:保存一个 Graphics 对象会存储它的状态,即缩放、模式、平移、剪辑等。所有您现在不需要担心的高级东西..)) - 您应该在 Paint 事件中进行绘制;然后你可以保存你用 DrawToBitmap 绘制的东西。或绘制成位图。有关任一方式的示例,请参见here。
标签: c# image visual-studio graphics draw