我有一个包含图像的画布,其中显示了现有的 BMP。我在画布上绘制矩形并将它们添加到 Children 集合中。当我单击保存时,我想更新底层 BMP 文件。
以下代码有效,但绘制到 BMP 的矩形比我绘制的要小得多。我猜坐标有一些差异?也许我不应该使用 System.Drawing?
using (Graphics g = Graphics.FromImage(image))
{
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
foreach (var child in canvas.Children)
{
if (child is System.Windows.Shapes.Rectangle)
{
var oldRect = child as System.Windows.Shapes.Rectangle;
// need to do something here to make the new rect bigger as the scale is clearly different
var rect = new Rectangle((int)Canvas.GetLeft(oldRect), (int)Canvas.GetTop(oldRect), (int)oldRect.Width, (int)oldRect.Height);
g.FillRectangle(Brushes.Black, rect);
}
}
... code to save bmp
欢迎所有建议!
谢谢