有沒有簡單的方法。最後我做相同的(一些修正):
public static Bitmap Draw(this IEnumerable<Point> points,
Color paintColor = default(Color))
{
return points.Draw(paintColor, Color.White);
}
public static Bitmap Draw(this IEnumerable<Point> points, Color paintColor,
Color backColor)
{
return points.Draw(paintColor, backColor, (ps, leftmost, topmost) => ps);
}
public static Bitmap DrawRelative(this IEnumerable<Point> points,
Color paintColor = default(Color))
{
return points.DrawRelative(paintColor, Color.White);
}
public static Bitmap DrawRelative(this IEnumerable<Point> points,
Color paintColor, Color backColor)
{
return points.Draw(paintColor, backColor,
(ps, leftmost, topmost) => ps.Select(p => new Point(p.X - leftmost.X, p.Y - topmost.Y)));
}
static Bitmap Draw(this IEnumerable<Point> points,
Color paintColor, Color backColor,
Func<IEnumerable<Point>, Point, Point, IEnumerable<Point>> transformer)
{
points = points.ToArray();
Func<IEnumerable<Point>, IEnumerable<Point>> horizontallSorter = ps => ps.OrderBy(p => p.X);
Func<IEnumerable<Point>, IEnumerable<Point>> verticallSorter = ps => ps.OrderBy(p => p.Y);
var leftmost = horizontallSorter(points).First();
var topmost = verticallSorter(points).First();
var relativePoints = transformer(points, leftmost, topmost).ToArray();
var rightmost = horizontallSorter(relativePoints).Last();
var bottommost = verticallSorter(relativePoints).Last();
var img = new Bitmap(rightmost.X + 1, bottommost.Y + 1);
using (var g = Graphics.FromImage(img))
{
g.Clear(backColor);
}
foreach (var item in relativePoints)
img.SetPixel(item.X, item.Y, paintColor);
return img;
}
Draw
方法沿points
輸入指定的確切座標繪製。 DrawRelative
從X軸和Y軸的第一個非白點開始繪製,即所有點都是相對移動,以便從左/上邊緣沒有空白。
使用Graphics.FromImage + Graphics.DrawLines –
@HansPassant,但從哪個圖像?我需要將'Image'參數傳遞給'Graphics.FromImage',但是如何獲取該圖像?如果我沒有寬度和高度參數,如何創建該圖像)。 – nawfal
當然你的位圖。位圖來自Image。 –