Bitmap 是Image的子类
因此,Bitmap类指针可以转换为Image类指针
下面的转换是没有问题的
Bitmap bmp(souceRectf.Width,souceRectf.Height);
Image*pImg=(Image*)&bmp;
但是,如果pImg是全局变量,也即pImg在上述函数体范围外继续使用时,便会出现错误。
原因是: bmp是局部变量,因此出了此函数作用范围后,便会被系统撤销,而这时如果再调用pImg便会出现读内存错误。
那如何解决呢?如何才能使得出来函数范围后,变量还可继续使用?
那当然是在堆上分配变量
方法一: 直接NEW Bitmap
Bitmap *pImg=new Bitmap(souceRectf.Width,souceRectf.Height);
Graphics graphics(pImg);
graphics.DrawImage(m_pImage,desRectf,souceRectf.X,souceRectf.Y,souceRectf.Width,souceRectf.Height,UnitPixel,NULL);
if (m_showImageDlg.pImg!=NULL)
{
delete m_showImageDlg.pImg;
}
m_showImageDlg.pImg=pImg; //