获取位图并将其绘制为图像 [英] Get bitmap and draw it into image
问题描述
我正在尝试将我的图片从图片框保存到位图中,然后将该位图绘制到图像中.到目前为止,最终图像中什么都没有出现,但是在调试时我只能说原始位图不是null且with/height是正确的.但是,当我将其绘制成图像后,什么也没有出现.
I am trying to save my drawing from picturebox into bitmap and draw that bitmap into image. So far, nothing has appeared in the final image, but while debugging I can only say, that the original bitmap is not null and with/height are correct. However nothing appears after I draw it into image.
我将图形保存到位图中,如下所示:
I save my drawing into bitmap like this:
GraphicsPath path = RoundedRectangle.Create(x, y, width, height, corners, RoundedRectangle.RectangleCorners.All);
g.FillPath(Brushes.LightGray, path);
g.SetClip(path);
using (Font f = new Font("Tahoma", 9, FontStyle.Bold))
g.DrawString(mtb_hotspotData.Text, f, Brushes.Black, textX, textY);
g.ResetClip();
bitmap = new Bitmap(width, height, g);
然后保存:
hs.bitmap = new Bitmap(bitmap);
最后使用它:
for (int i = 0; i < imageSequence.Count; i++) {
Graphics g = Graphics.FromImage(imageSequence[i]);
//g.CompositingMode = CompositingMode.SourceOver;
//hotspot.bitmap.MakeTransparent();
int x = hotspot.coordinates[i].X;
int y = hotspot.coordinates[i].Y;
g.DrawImage(hotspot.bitmap, new Point(x, y));
}
return imageSequence;
到目前为止,我在该解决方案中找不到任何问题,因此我不知道故障在哪里.
So far I was not able to find any problem in this solution, therefore I have no idea, where the malfunction is.
推荐答案
您似乎误解了Bitmap
和Graphics
对象的关系.
You seem to misunderstand the relation of a Bitmap
and a Graphics
object.
-
Graphics
对象不包含任何图形;它是用于将插入某种位图的工具.
A
Graphics
object does not contain any graphics; it is a tool used to draw into a bitmap of some sort.
位图构造器您正在使用(public Bitmap(int width, int height, Graphics g)
)确实不真正 connect Bitmap
和Graphics
对象.它仅使用Graphics
中的dpi
分辨率.
The Bitmap constructor you are using (public Bitmap(int width, int height, Graphics g)
) does not really connect the Bitmap
and the Graphics
object. It only uses the dpi
resolution from the Graphics
.
您不会显示Graphics
的创建方式.如果要绘制到Bitmap
(而不是控件的表面)中,最直接的方法是:
You don't show how your Graphics
is created. If you want to draw into a Bitmap
(as opposed to a control's surface) the most direct way is this:
Bitmap bitmap = new Bitmap(width, height);
bitmap.SetResolution(dpiX, dpiY); // optional
using (Graphics G = Graphics.FromImage(bitmap ))
{
// do the drawing..
// insert all your drawing code here!
}
// now the Bitmap can be saved or cloned..
bitmap.Save(..);
hs.bitmap = new Bitmap(bitmap); // one way..
hs.bitmap = bitmap.Clone(); // ..or the other
// and finally disposed of (!!)
bitmap.Dispose();
这篇关于获取位图并将其绘制为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!