最近我开始研究我的项目,不幸的是我遇到了问题。我想从一个图像中获取sqaures 5x5,计算它们的平均颜色,然后将圆圈绘制到另一个Bitmap,这样我就可以获得这样的结果http://imageshack.com/a/img924/9093/ldgQAd.jpg
我已经完成了,但我无法保存到文件Graphics对象。我尝试过Stack的很多解决方案,但没有一个能为我工作。
我的代码:
//GET IMAGE OBJECT
Image img = Image.FromFile(path);
Image newbmp = new Bitmap(img.Width, img.Height);
Size size = img.Size;
//CREATE NEW BITMAP WITH THIS IMAGE
Bitmap bmp = new Bitmap(img);
//CREATE EMPTY BITMAP TO DRAW ON IT
Graphics g = Graphics.FromImage(newbmp);
//DRAWING...
//SAVING TO FILE
Bitmap save = new Bitmap(size.Width, size.Height, g);
g.Dispose();
save.Save("file.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
文件'file.bmp'只是一张空白图片。我做错了什么?
答案 0 :(得分:1)
首先,应该从目标位图创建Graphics对象。
Bitmap save = new Bitmap(size.Width, size.Height) ;
Graphics g = Graphics.FromImage(save );
其次,在保存()
之前刷新图形g.Flush() ;
最后,在Save()之后Dispose()(或使用using块)
save.Save("file.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
g.Dispose();
它应该给你这样的东西:
Image img = Image.FromFile(path);
Size size = img.Size;
//CREATE EMPTY BITMAP TO DRAW ON IT
using (Bitmap save = new Bitmap(size.Width, size.Height))
{
using (Graphics g = Graphics.FromImage(save))
{
//DRAWING...
//SAVING TO FILE
g.Flush();
save.Save("file.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
}