Recently made a public service, there are some simple image processing functions. Mainly is the user in the page operation, the front-end to do some immediate display of the effect, and then commit to save the back end really modify the original image.
Our backend is, that is, the ASP.NET
C#
language, C#
itself processing the picture is more convenient, the use GDI+
is good, only need to add a System.Drawing
reference, do not need any third-party library. So recently also used some of the more commonly used GDI+
image processing methods, the collation of a record.
This topic will probably write a few articles, the first simple introduction GDI+
of the common objects, as well as some of the use of time considerations, the following will pick some of the more useful process to introduce the project.
Don't say much nonsense, start to get to the point.
Classes that need to be used
GDI+
There are several commonly used classes used in drawing: Graphics
, Bitmap
, Image
.
Which Graphics
is the artboard. This class contains many methods of drawing, including drawing pictures (), drawing DrawImage
lines (), DrawLine
drawing Circles ( DrawEllipse、FillEllipse
), writing (), and DrawString
so on. Simply put, using this class can do most of the work we need.
Generate an Graphics
object that needs to be used Image
or Bitmap
.
PS: Winform
You can refer to an object directly from the event of a form or control Graphics
.
Like what:
private void Form1_Paint(object sender, PaintEventArgs e){ Graphics g = e.Graphics; // 创建画板,这里的画板是由Form提供的.}
However, this article discusses other scenarios, such as ASP.NET MVC
, or simply, console programs. There are no controls at these times, so use a different method.
I generally use the following methods:
//Summary:Creates a new System.Drawing.Graphics from the specified System.Drawing.Image.////parameters: //image://the System.Drawing.Image from which to create the new System.Drawing.Graphics. ////return Result: //This method for the specified System.Drawing.Image returns a new System.Drawing.Graphics. ////exception: //T: System.argumentnullexception://image is null. ////t:system.exception:// The image has an indexed pixel format, or the format is undefined. public static Graphics fromimage (image Image);
The arguments can be passed in Image
or Bitmap
, because they Bitmap
are inherited from Image
.
How to create Artboards
If you want to process the original image, such as rotating pictures, add text, etc., you can get the artboard object directly from the original picture.
Image img = Image.FromFile(imgPath);Graphics graphics = Graphics.FromImage(img);
If you want to draw a new diagram, you can create an artboard with the width and height of the picture you want to save.
new Bitmap(width, height);Graphics graph = Graphics.FromImage(bmp);
PS: Graphics
itself is not provided as a constructor to generate directly. So we can first create a bitmap object that needs to save the image size before we Bitmap
get the artboard object.
How to save a picture of a good picture
img.Save(savePath)
bmp.Save(savePath)
You can save an object by calling or saving it.
PS: Bitmap
The Save
method is directly inherited from Image
the.
GDI+
The coordinate system
GDI+
Coordinate system is a two-dimensional coordinate system, but a little different, its origin is in the upper left corner. Such as:
Use
GDI+
A few things to note
Here I can not help but to vomit a groove, GDI+
the error message is not very friendly ah. Often just returns a "generic error occurred in GDI +." ", you cannot quickly locate the problem based on this error message. For example, if you do not release the image resources to re-access the resources will be reported this error, you want to save the picture folder does not exist when the error is also prompted. Can't see the difference ...
1. Save the image resource to the same path file, or you will get an error (general error occurred in GDI +)
new Bitmap(img);Graphics graphics = Graphics.FromImage(bmp);... // 对图片进行一些处理img.Dispose(); // 释放原图资源bmp.Save(imgPath); // 保存到原图graphics.Dispose(); // 图片处理过程完成,剩余资源全部释放bmp.Dispose();
2. Use up resources remember to release. can be used try..catch..finally
or using
the way, so even if the code run error can release resources in a timely manner, more insurance.
try..catch...finally
: Write the code that frees the resource to the finally
code snippet.
new Bitmap(img);Graphics graphics = Graphics.FromImage(bmp);try{ // ...}catch (System.Exception ex){throw ex;}finally{graphics.Dispose();bmp.Dispose();img.Dispose();}
using
: using
A resource created with a statement using
automatically frees the resource when it leaves the code snippet.
///<summary>Zoom image///</summary>///<param name= "Originalimagepath" > Original path</param>///<param name= "Destwidth" > Target graph width </param>///<param name= "DestHeight" > Target graph height Span class= "Hljs-doctag" ></param>///<returns></returns>public Bitmap getthumbnail ( string Originalimagepath, int destwidth, int destheight) {using (Image imgsource = Image.FromFile (OriginalImagePath) {return getthumbnail (Imgsource, Destwidth, destheight);}}
3. The folder where you want to save the picture must already exist, or it will be an error (generic error occurred in GDI +)
Eg: Suppose the picture is to be saved toD:\test\output.png
string directory = @"D:\test\";string fileName = "output.png";// 检查文件夹是否存在,不存在则先创建if (!Directory.Exists(directory)){ Directory.CreateDirectory(directory);}bmp.Save(directory + fileName);
C # using GDI + Paint