C#控制台应用程序-如何使用GDI +绘制BMP/JPG文件? [英] C# Console Application - How to draw in BMP/JPG file using GDI+?

查看:192
本文介绍了C#控制台应用程序-如何使用GDI +绘制BMP/JPG文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C#控制台应用程序和GDI +在BMP或JPG文件中绘制矩形,箭头,文本,线条等形状.这是我在网上找到的:

I want to draw shapes like rectangles, arrows, text, lines in a BMP or JPG file, using a C# Console Application and GDI+. This is what I found on the web:

c#将System.Drawing.Graphics保存到文件 c#保存系统. Drawing.Graphics归档 GDI +初学者教程 http://www.c-sharpcorner.com/UploadFile/mahesh/gdi_plus12092005070041AM/gdi_plus.aspx 专业C#-使用GDI +的图形codeproject.com/Articles/1355/Professional-C-Graphics-with-GDI

c# save System.Drawing.Graphics to file c# save System.Drawing.Graphics to file GDI+ Tutorial for Beginners http://www.c-sharpcorner.com/UploadFile/mahesh/gdi_plus12092005070041AM/gdi_plus.aspx Professional C# - Graphics with GDI+ codeproject.com/Articles/1355/Professional-C-Graphics-with-GDI

但这仍然无济于事.其中一些链接仅针对Windows窗体应用程序对此进行了说明,而其他链接仅作为参考(MSDN链接),仅解释了GDI +中的类,方法等. 那么如何使用C#控制台应用程序绘制图片文件? 谢谢!

But this still doesn't help me. Some of these links explains this only for a Windows Forms Application and other links are only for reference (MSDN links), explaining only classes, methods etc. in GDI+. So how can I draw in a picture file using a C# Console Application? Thank you!

推荐答案

在控制台模式的应用程序中创建位图非常简单.只是一个小绊脚石,项目模板并没有预先选择您需要的.NET程序集.项目+添加引用,选择System.Drawing

It is pretty straight-forward to create bitmaps in a console mode app. Just one minor stumbling block, the project template doesn't preselect the .NET assembly you need. Project + Add Reference, select System.Drawing

一个非常简单的示例程序:

A very simple example program:

using System;
using System.Drawing;   // NOTE: add reference!!

class Program {
    static void Main(string[] args) {
        using (var bmp = new Bitmap(100, 100))
        using (var gr = Graphics.FromImage(bmp)) {
            gr.FillRectangle(Brushes.Orange, new Rectangle(0, 0, bmp.Width, bmp.Height));
            var path = System.IO.Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                "Example.png");
            bmp.Save(path);
        }
    }
}

运行此命令后,您的桌面上将有一个新的位图.它是橙色的.利用Graphics方法发挥创意,使其看起来像您想要的样子.

After you run this you'll have a new bitmap on your desktop. It is orange. Get creative with the Graphics methods to make it look the way you want.

这篇关于C#控制台应用程序-如何使用GDI +绘制BMP/JPG文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆