Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

Posted

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!

Posted
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?

I'm trying to crop only the transparent region and create a new image out of it.

  • Leaders
Posted

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.

[sIGPIC]e[/sIGPIC]
Posted
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.

Posted
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);

  • Leaders
Posted
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?
[sIGPIC]e[/sIGPIC]
Posted
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!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...