How to save TransParent IMage after drawing on another image?
-
Hello Friends I am using GDIplus to save an Image.I am getting the Graphics reference from Image not from Device.And then m drawing another image of same size (White background with one Red Rectangle) by using DrawImage method of Graphics. Here is the code below:
CString FileName = "c:\\temp.TIF";
LPCSTR cstr=FileName;
int maxlen=FileName.GetLength()*2;
WCHAR *str=new WCHAR[maxlen];
MultiByteToWideChar(CP_ACP,0,cstr,-1,str,maxlen);
Image image(cstr, FALSE);
Graphics g(&image);CString str = "c:\\rectangle.TIF";
Bitmap bmp(str);ImageAttributes attr;
Gdiplus::ColorMatrix cm =
{
1.0f, 0.0f, 0.0f, 0, 0,
0.0f, 1.0f, 0.0f, 0, 0,
0.0f, 0.0f, 1.0f, 0, 0,
0.0f, 0.0f, 0.0f, 0.6f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f
};
attr.SetColorMatrix(&cm, Gdiplus::ColorMatrixFlagsDefault,Gdiplus::ColorAdjustTypeBitmap);
g.DrawImage(&bmp,Gdiplus::Rect(0, 0, bmp.GetWidth(), bmp.GetHeight()), 0,0, bmp.GetWidth(), bmp.GetHeight(),UnitPixel,&attr);
bmp.Save(str,pClsid,NULL);I used ImageAttributes for Transparency but by using setColorMatrix,It sets transparency for whole image. Now,I have two problems: 1)I want to set transparency for White pixels so that when I draw rectangle.tif on temp.tif file I can see background.I want to use as layer. 2)When I draw rectangle with ImageAttribute on temp file and then its not saving with transparency info even. Any Ideas Thanks In Advance. Regards Yogesh
-
Hello Friends I am using GDIplus to save an Image.I am getting the Graphics reference from Image not from Device.And then m drawing another image of same size (White background with one Red Rectangle) by using DrawImage method of Graphics. Here is the code below:
CString FileName = "c:\\temp.TIF";
LPCSTR cstr=FileName;
int maxlen=FileName.GetLength()*2;
WCHAR *str=new WCHAR[maxlen];
MultiByteToWideChar(CP_ACP,0,cstr,-1,str,maxlen);
Image image(cstr, FALSE);
Graphics g(&image);CString str = "c:\\rectangle.TIF";
Bitmap bmp(str);ImageAttributes attr;
Gdiplus::ColorMatrix cm =
{
1.0f, 0.0f, 0.0f, 0, 0,
0.0f, 1.0f, 0.0f, 0, 0,
0.0f, 0.0f, 1.0f, 0, 0,
0.0f, 0.0f, 0.0f, 0.6f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f
};
attr.SetColorMatrix(&cm, Gdiplus::ColorMatrixFlagsDefault,Gdiplus::ColorAdjustTypeBitmap);
g.DrawImage(&bmp,Gdiplus::Rect(0, 0, bmp.GetWidth(), bmp.GetHeight()), 0,0, bmp.GetWidth(), bmp.GetHeight(),UnitPixel,&attr);
bmp.Save(str,pClsid,NULL);I used ImageAttributes for Transparency but by using setColorMatrix,It sets transparency for whole image. Now,I have two problems: 1)I want to set transparency for White pixels so that when I draw rectangle.tif on temp.tif file I can see background.I want to use as layer. 2)When I draw rectangle with ImageAttribute on temp file and then its not saving with transparency info even. Any Ideas Thanks In Advance. Regards Yogesh
I hope i understand your questions clearly. Im not using GDIplus (but GDI) and Im not using your program language, but maybe this helps a little: - to get transparency on the pixellevel every pixel in your bitmap must be 4 bytes (RGBA) and not 3 bytes (RGB). See MSDN for exact info. In the A byte (called alpha channel) you may set the transparency level between 0 and 255. You should use the winapi function Alphablend() (or the gdiplus equevalent) to copy the bitmap to the screen. - One of the problems with transparent bitmaps is that when you put them on screen the transparency is visible but this information is lost (on the screen). Let me explain this: Suppose you want to fade in a bitmap. Then for every step you must first repaint the background (that what wad on the screen before the bitmap was painted on it) and then copy the transparent bitmap on it, decreasing transparency in every step. If you don't repaint the background first you will not get the desired result (but a transparent bitmap on a tranparent bitmap). The screen has no transparency set (it is a RGB-surface) so saving it will result in a bitmap that only shows 'what was on screen'. Transparency is a property of a bitmap not of the screen that displays it (the screen shows the result only). Hope this helps a bit... Good luck, Rozis
-
I hope i understand your questions clearly. Im not using GDIplus (but GDI) and Im not using your program language, but maybe this helps a little: - to get transparency on the pixellevel every pixel in your bitmap must be 4 bytes (RGBA) and not 3 bytes (RGB). See MSDN for exact info. In the A byte (called alpha channel) you may set the transparency level between 0 and 255. You should use the winapi function Alphablend() (or the gdiplus equevalent) to copy the bitmap to the screen. - One of the problems with transparent bitmaps is that when you put them on screen the transparency is visible but this information is lost (on the screen). Let me explain this: Suppose you want to fade in a bitmap. Then for every step you must first repaint the background (that what wad on the screen before the bitmap was painted on it) and then copy the transparent bitmap on it, decreasing transparency in every step. If you don't repaint the background first you will not get the desired result (but a transparent bitmap on a tranparent bitmap). The screen has no transparency set (it is a RGB-surface) so saving it will result in a bitmap that only shows 'what was on screen'. Transparency is a property of a bitmap not of the screen that displays it (the screen shows the result only). Hope this helps a bit... Good luck, Rozis