如何确定 Drawing.Graphics 对象与给定的 Bitmap 对象相关?

发布于 2025-01-07 02:45:24 字数 596 浏览 3 评论 0原文

我有一组位图和一组图形。最初,每个 Bitmap 对象在这些数组中都有相应的 Graphics 对象。但是在工作期间,数组中的位图可能会被新实例替换或更改它们在数组中的位置。 所以我需要一种方法来找到与给定 Bitmap 对象相对应的正确 Graphics 对象(或者确保图形数组中没有相应的 Graphics 对象)。

请查看 C# 中的示例代码:

void Main()
{
   // make some bitmaps
   Bitmap b1 = new Bitmap(100,100);
   Bitmap b2 = new Bitmap(100,100);

   // make graphics of our bitmaps
   Graphics g1 = Graphics.FromImage(b1);
   Graphics g2 = Graphics.FromImage(b2);

   bool result = Test(b1, g2);
}

bool Test(Bitmap b, Graphics g)
{
   // how can I check that given "g" is really created from given "b"?
   // ???
}

I have a array of Bitmaps and a array of Graphics. Initially each Bitmap object have our corresponding Graphics object in these arrays. But during the work bitmaps in array may be replaced by new instances or change their positions in array.
So I need a way to find proper Graphics object that corresponding to given Bitmap object (or to make sure that is no corresponding Graphics object in graphics-array).

please look this example code in C#:

void Main()
{
   // make some bitmaps
   Bitmap b1 = new Bitmap(100,100);
   Bitmap b2 = new Bitmap(100,100);

   // make graphics of our bitmaps
   Graphics g1 = Graphics.FromImage(b1);
   Graphics g2 = Graphics.FromImage(b2);

   bool result = Test(b1, g2);
}

bool Test(Bitmap b, Graphics g)
{
   // how can I check that given "g" is really created from given "b"?
   // ???
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2025-01-14 02:45:24

如果图形是在中心位置创建的,您可以使用字典 Dictionary 维护一个查找表。

或者,您还可以使用 Bitmap 的 Tag 属性来存储对 Graphics 对象的引用。

   Bitmap b1 = new Bitmap(100,100); 
   Bitmap b2 = new Bitmap(100,100); 

   // make graphics of our bitmaps 
   Graphics g1 = Graphics.FromImage(b1); 
   Graphics g2 = Graphics.FromImage(b2); 

   // Track the graphics object for each bitmaps
   // NOTE: be sure to dispose the Graphics when you're done with the Bitmap.
   b1.Tag = g1;
   b2.Tag = g2;

无论如何,请确保在不再需要 Graphics 对象时正确处理它们。

If the graphics are being created in a central place you could maintain a lookup table using a dictionary, Dictionary<Bitmap, Graphics>.

Alternatively, you could also use the Tag property of the Bitmap to store a reference to the Graphics object.

   Bitmap b1 = new Bitmap(100,100); 
   Bitmap b2 = new Bitmap(100,100); 

   // make graphics of our bitmaps 
   Graphics g1 = Graphics.FromImage(b1); 
   Graphics g2 = Graphics.FromImage(b2); 

   // Track the graphics object for each bitmaps
   // NOTE: be sure to dispose the Graphics when you're done with the Bitmap.
   b1.Tag = g1;
   b2.Tag = g2;

In any case, make sure you are properly disposing of your Graphics objects once you no longer need them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文