Hello
here is the full code in which I m applying transparency to images.
void applyTransparencyToLayers(CString& fullName)
{
CString tempTiff = "c:\\sample.TIF";
LPCSTR cstr=tempTiff;
int maxlen=tempTiff.GetLength()*2;
WCHAR *str=new WCHAR[maxlen];
MultiByteToWideChar(CP_ACP,0,cstr,-1,str,maxlen);
Bitmap bmp(str);
Bitmap loadedImage(bmp.GetWidth(),bmp.GetHeight(),PixelFormat32bppARGB);
Rect rect(0, 0, loadedImage.GetWidth(),loadedImage.GetHeight());
Graphics g(&loadedImage);
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.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f, 1.0f
};
attr.SetColorMatrix(&cm, Gdiplus::ColorMatrixFlagsDefault,Gdiplus::ColorAdjustTypeBitmap);
g.DrawImage(&loadedImage,rect, 0,0, loadedImage.GetWidth(), loadedImage.GetHeight(),UnitPixel,&attr);
Bitmap originalMaskImage(bmp.GetWidth(),bmp.GetHeight(),PixelFormat32bppARGB);
Rect rect1(0, 0, originalMaskImage.GetWidth(),originalMaskImage.GetHeight());
Graphics g1(&originalMaskImage);
g1.DrawImage(&originalMaskImage,rect1, 0,0, originalMaskImage.GetWidth(), originalMaskImage.GetHeight(),UnitPixel,&attr);
BitmapData bmpData;
originalMaskImage.LockBits(rect1, ImageLockModeRead, PixelFormat32bppARGB,&bmpData);
int dataLen = bmpData.Stride * bmpData.Height;
UINT* pixels = (UINT*)bmpData.Scan0;
BYTE* maskImageRGBData;
maskImageRGBData = new BYTE[dataLen];
memcpy(maskImageRGBData,pixels,dataLen);
byte greyLevel;
bool opaque = true;
int OpacityThreshold = 255;
bool invertedMask = true;
for (int i = 0; i + 2 < dataLen; i += 4)
{
greyLevel = (byte)(0.3 * maskImageRGBData[i + 2] + 0.59 * maskImageRGBData[i + 1] + 0.11 * maskImageRGBData[i]);
if (opaque)
{
greyLevel = (greyLevel < OpacityThreshold) ? 0 : 255;
}
if (invertedMask)
{
greyLevel = (byte)(255 - (int)greyLevel);
}
maskImageRGBData[i] = greyLevel;
maskImageRGBData[i + 1] = greyLevel;
maskImageRGBData[i + 2] = greyLevel;
}
memcpy(bmpData.Scan0,maskImageRGBData,dataLen);
originalMaskImage.UnlockBits(&bmpData);
Bitmap maskedImage(bmp.GetWidth(),bmp.GetHeight(),PixelFormat32bppARGB);
Rect rect2(0, 0, maskedImage.GetWidth(),maskedImage.GetHeight());
Graphics g2(&maskedImage);
g2.DrawImage(&maskedImage,rect2, 0,0, maskedImage.GetWidth(), maskedImage.GetHeight(),UnitPixel,&attr);
BitmapData bmpData1;
maskedImage.LockBits(rect2, ImageLockModeRead, PixelFormat32bppARGB,&bmpData1);
int dataLen1 = bmpData1.Stride * bmpData1.Height;
byte* maskedImageRGBAData;
maskedImageRGBAData = new byte[dataLen1];
memcpy(maskedImageRGBAData,bmpData1.Scan0,dataLen1);
BitmapData bmpData2;
originalMaskImage.LockBits(rect1, ImageLockModeRead, PixelFormat32bppARGB,&bmpData2);
int dataLen2 = bmpData2.Stride * bmpData2.Height;
byte* maskImageRGBAData;
maskImageRGBAData = new byte[dataLen2];
memcpy(maskImageRGBAData,bmpData2.Scan0,dataLen2);
for (int j = 0; j + 2 < dataLen1; j += 4)
{
maskedImageRGBAData[j + 3] = maskImageRGBAData[j];
}
memcpy(bmpData1.Scan0,maskedImageRGBAData,dataLen1);
maskedImage.UnlockBits(&bmpData1);
originalMaskImage.UnlockBits(&bmpData2);
CLSID* pClsid = new CLSID;
UINT num;
UINT size;
ImageCodecInfo* pImageCodecInfo=NULL;
GetImageEncodersSize(&num,&size);
pImageCodecInfo=(ImageCodecInfo*)(malloc(size));
GetImageEncoders(num,size,pImageCodecInfo);
for(UINT k=0;k<num;k++)
{
if(wcscmp(pImageCodecInfo[k].MimeType,L"image/tiff")==0)
{
*pClsid = pImageCodecInfo[k].Clsid;
break;
}
}
LPCSTR lName=fullName;
int maxlen1=fullName.GetLength()*2;
WCHAR *str1=new WCHAR[maxlen1];
MultiByteToWideChar(CP_ACP,0,lName,-1,str1,maxlen1);
maskedImage.Save(str1,pClsid,NULL);
}
Regards
Yogesh
|