Apply ScaleTransform to Graphics GDI+

孤街醉人 提交于 2019-12-10 19:17:23

问题


I have put this simple code together to draw a line. Now I want to apply a ScaleTransform to it by a factor of 10; but the code below doesn't work.

var bitmap = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
var g = Graphics.FromImage(bitmap);
pictureBox1.Image = bitmap;

var pn = new Pen(Color.Wheat, -1);
g.DrawLine(pn, 0, 0, 10, 10);

pn.Dispose();

// I'm trying to scaletransform here!
g.ScaleTransform(10, 10);

Update:

What is the correct way to update the changes? I'm not getting any results from this :(

g.ScaleTransform(1, 1);
pictureBox1.Invalidate();

回答1:


You must apply the transformation BEFORE drawing the line!

var g = Graphics.FromImage(bitmap);
g.ScaleTransform(10, 10);    
using (pn = new Pen(Color.Wheat, -1)) {
    g.DrawLine(pn, 0, 0, 10, 10);
}

Transformations are applied to the transformation matrix of the graphics object (g.Transform).

Also make use of the using statement in order to dispose the resources. It will even dispose the pen if an exception should occur or if the using statement-block should be left with a return or break statement.



来源:https://stackoverflow.com/questions/22615138/apply-scaletransform-to-graphics-gdi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!