Copy transparent areas from PNG file

midasab

Newcomer
Joined
Sep 25, 2009
Hi,

I've one png file which has 2 transparent areas one is square and the other is circle. Now I need to create 2 new png files for these transparent areas. Like copying only square one on another black or any color background and the same thing for circle area.

I don't want to crop a rectangle since I have a circle shape and if square falls in that rectangle area I won't get the proper transparent area.

I don't know how to determine only transparent regions and how to separate each region?!!

Can anybody help me about that? Any suggestions?:confused:
 

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Location
USA
I'm not clear on what you are trying to do. Are you just trying to draw the image? Or create a new image that is a cropped piece of the original?
 

midasab

Newcomer
Joined
Sep 25, 2009
Thanks for the reply!

I want to end up with 2 new images.

My problem is with overlapping transparent areas for shapes other than rectangle. I have rectangle coordinates of these areas but for any shape other than rectangle, I cannot crop a rectangle because it will contain parts of the other area that I've already copied.


I've attached an example image to describe what I'm looking for. This is not the actual graphic that I'm working with but I thought it explains what I'm looking for. Imagine the gray areas are the transparent.

I want to be able to copy inside area of rectangle up to the red border and create a new image (image1) with black background and paste that area in there, then again copy inside of the oval and create an image (image2) with the black background and paste this oval area there.

Is it possible to do this? Any ideas?:confused:


Thanks in advance!
 

Attachments

  • example.PNG
    2.1 KB · Views: 23

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Location
USA
So you basically need to copy non-regtangular regions of pixels defined as the transparent area of a PNG?

Assuming I understand correctly, you probably need to manipulate the raw pixel data of the images, scanning the "mask" png and copying pixels from the source to the destination when you encounter transparent pixels.

Another option, if you are creating the masks yourself, could be to clone the original image (or the desired rectangular region), paste the mask with a solid color over the pixels you don't want, and use the Bitmap.MakeTransparent() method to exclude the masked region.
 

midasab

Newcomer
Joined
Sep 25, 2009
So you basically need to copy non-regtangular regions of pixels defined as the transparent area of a PNG?

Assuming I understand correctly, you probably need to manipulate the raw pixel data of the images, scanning the "mask" png and copying pixels from the source to the destination when you encounter transparent pixels.

Another option, if you are creating the masks yourself, could be to clone the original image (or the desired rectangular region), paste the mask with a solid color over the pixels you don't want, and use the Bitmap.MakeTransparent() method to exclude the masked region.
Thank you! I've created a mask which only takes the transparent areas and changes the color to black and the background is transparent. Now my question is how to separate these 2 back masses and save them in different files. I've attached a file to explain what I'm looking for. This is a sample I've created and gray area is the transparent area.
Can you please guide me how to separate them? I don’t know what kind of shapes are they I will have 2 separate black areas that I need to save them in 2 different files.

I would appreciate any help.

Thanks again.
 

midasab

Newcomer
Joined
Sep 25, 2009
Here is the file. I will post the code that creates this mask tomorrow.
 

Attachments

  • mask.PNG
    1.8 KB · Views: 22

midasab

Newcomer
Joined
Sep 25, 2009
Here is the file. I will post the code that creates this mask tomorrow.

Here is the code:

Int32Rect cropRect = new Int32Rect(0, 0, (Int32)_image.Width, (Int32)_image.Height);

CroppedBitmap crBitmap = new CroppedBitmap(_image, cropRect);

// Create an Alpha mask BitmapSource.
Int32 sourceDefaultMaskRawStride = (crBitmap.PixelWidth * crBitmap.Format.BitsPerPixel + 7) / 8;

byte[] sourceDefaultMaskPixels = new byte[sourceDefaultMaskRawStride * crBitmap.PixelHeight];

crBitmap.CopyPixels(sourceDefaultMaskPixels, sourceDefaultMaskRawStride, 0);

for (int j = 0; j < sourceDefaultMaskPixels.Count(); j += 4)
{
sourceDefaultMaskPixels[j + 3] = (byte)(255 - (int)sourceDefaultMaskPixels[j + 3]);
}

BitmapSource alphaDefaultMask = BitmapSource.Create(
crBitmap.PixelWidth,
crBitmap.PixelHeight,
96, 96,
PixelFormats.Pbgra32, null,
sourceDefaultMaskPixels, sourceDefaultMaskRawStride);
 

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Location
USA
I'm still not clear on where/how/why the masks are being generated. Can't you just generate different images int the first place instead of creating a single mask and trying to separate them out?
 

midasab

Newcomer
Joined
Sep 25, 2009
I'm still not clear on where/how/why the masks are being generated. Can't you just generate different images int the first place instead of creating a single mask and trying to separate them out?

I can't because I only have png files and coordinates of the masks on them. My application goes through png files and uses the coordinates to create alpha masks for them. It works for most of them fine. I only have problem with overlapping masks that contain a shape other than square or rectangle.

I don’t know how to handle them in my code or even it is possible.

Thank you!
 
Top Bottom