Как определить относится ли объект Graphics к данному Bitmap - C#
Формулировка задачи:
Привет все!
Например есть вот такой код:
void Main()
{
Bitmap b1 = new Bitmap(100,100);
Bitmap b2 = new Bitmap(100,100);
Graphics g1 = Graphics.FromImage(b1);
Graphics g2 = Graphics.FromImage(b2);
bool result = Test(b1, g2);
}
bool Test(Bitmap b, Graphics g)
{
// как определить является ли "g" графикой для картинки "b" или нет
// ???
}Решение задачи: «Как определить относится ли объект Graphics к данному Bitmap»
textual
Листинг программы
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Bitmap b1 = new Bitmap(100, 100);
Bitmap b2 = new Bitmap(100, 100);
Graphics g1 = Graphics.FromImage(b1);
b1.Tag = g1;
Graphics g2 = Graphics.FromImage(b2);
b2.Tag = g2;
bool result1 = Test(b1, g2);
bool result2 = Test(b1, g1);
}
private bool Test(Bitmap bmp, Graphics gr)
{
bool result = false;
Graphics grBmp = bmp.Tag as Graphics;
if (grBmp != null)
{
if (object.ReferenceEquals(grBmp, gr))
result = true;
}
return result;
}
}