C# /GDI Graphics Scaling (Drawline)
I'm writing something to create small bitmap previews from a vector format file. From the file I have a (large) list of line coordinates. What I want to do is scale it to make it fit in a fixed-width image (96x96). I had previously been drawing the bitmap at full size and then just resizing it to 96x96 but since the thumbnails needed to be created on the fly that's turned out to not be fast enough (and it was a really dumb way to do it in the first place!). Now I just want to scale all of the coordinates as if the original size was 96x96, drop all of the points that draw on top of each other and that should greatly increase performance.
I'm an absolute newbie with any and all of the .NET Graphics/GDI stuff and the first version was pretty simple (code below). I'm wondering if there is something in the Graphics library (or elsewhere) that does that without me having to loop through all of the points and do the math on each one.
Can a Graphics/GDI guru point me in the right direction (or let me know there isn't a direction)? I'm using C#, and .NET framework target is OK.
So far it's pretty simple (tmpblocks is an array of points):
Bitmap DrawArea;
Graphics xGraph;
DrawArea = new Bitmap(64, 64);
// ^- this is GetWidth() and GetHeight() when drawing the full file at full size
xGraph = Graphics.FromImage(DrawArea);
for (int i = 0; i < tmpblocks.Count; i++)
{
if (tmpblocks[i].stitches.Length > 1)
{
Pen tempPen = new Pen(tmpblocks[i].color,开发者_高级运维 1.0f);
tempPen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
tempPen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
tempPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
xGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
xGraph.DrawLines(tempPen, tmpblocks[i].stitches);
}
}
For the thumbnail I'd just scale the resulting bitmap using the .GetThumbnail method. Really slow, though (obviously)...
You can use the Graphics.ScaleTransform(float sx, float sy)
to accomplish this.
You can get sx from: TargetWidth / SourceWidth and sy from: TargetHeight / SourceHeight
where the target is defined by your target image size, and source is your source image size.
精彩评论