简体   繁体   English

如何在不使用GDI +的c#中使用控件获取图形对象?

[英]How to get Graphic's object with out using a control in c# using GDI+?

I need to create a drawing with a circle and text embeded to it .This drawing is outputted as Image file (Jpeg/Jpg/svg/Png) using c# GDI+ . 我需要创建一个带有圆圈并嵌入文本的图形。此图形使用c#GDI +作为图像文件(Jpeg / Jpg / svg / Png)输出。

Since i dont want to display the UI directly on a form ,how do i get the graphic's object to start drawing. 由于我不想直接在窗体上显示UI,因此如何获取图形的对象来开始绘制。

Thanks in advance. 提前致谢。

You can create a new Bitmap passing the size for the image: 您可以创建一个新的Bitmap传递图像的大小:

using (Bitmap myBitmap = new Bitmap(100, 100)) {
    using (Graphics g = Graphics.FromImage(myBitmap)) {
        // do your drawing here
    }

    myBitmap.Save(@"C:\path\for\image.bmp");
}

Optionally you can set the ImageFormat for the image when saving (可选)您可以在保存时为图像设置ImageFormat

myBitmap.Save(@"C:\path\for\image.png", ImageFormat.Png);

You create a Bitmap, and get the graphics object from it to draw on: 您创建一个位图,并从中获取图形对象以进行绘制:

Bitmap myBitmap = new Bitmap(@"C:\MyImg.bmp");
Graphics g = Graphics.FromImage(myBitmap);

Note that the Bitmap does not need to be created on disk, it can be created in memory too! 注意, 不需要在磁盘上创建Bitmap ,也可以在内存中创建Bitmap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM