c# - 如何使用 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 文件并确定文件大小。谢谢!!!
解决方案
你可以试试这行代码:
pictureBox1.Image.Save(@"Path",ImageFormat.Jpeg);
推荐阅读
- finite-automata - 非确定性有限自动机接受和拒绝
- angular - 当 mat-toogle-button 为 YES 时,我需要在输入下方显示警告消息吗?
- python - 曼哈顿距离的距离变换 - Python / NumPy / SciPy
- vue.js - 导航有交钥匙解决方案吗?
- apache-spark - 火花写镶木地板非常慢
- c++ - 我可以用 null 初始化一个迭代器吗?如果不是,为什么?
- mysql-8.0 - 错误代码:3685。正则表达式的非法参数
- qt - 不能 qmake QtWebengine - “qtwebenginecore-config.pri:没有这样的文件或目录”
- node.js - WebSocket 握手期间出错:意外的响应代码:https 上的 400
- angular - 如何将逗号分隔值拆分为角度表格