首页 > 解决方案 > 如何使用 C# 保存图像文件?

问题描述

我正在使用 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 文件并确定文件大小。谢谢!!!

标签: c#imagevisual-studiographicsdraw

解决方案


你可以试试这行代码:

pictureBox1.Image.Save(@"Path",ImageFormat.Jpeg);

推荐阅读