Troubled saving a clipped region from an image to disk as transparent

  • Thread starter Thread starter Mark Denardo
  • Start date Start date
M

Mark Denardo

Hi, I need some expert GDI+ person to help me with my RoundOffImage
Function:

What I'm trying to do is take in an image, crop off the edges around an
ellipse region I set up, and then return the cropped image from the
function. I sort of have this working, but not thoroughly.

If I take the output image of this function and draw it on my form it shows
the clipped image as transparent as I am wanting it. But if I take that
image and save it to disk:
m_RoundedCroppedImage.Save(....)

Then the clipped region is showing up as black in the saved file. What is
going on and how do I make it transparent and not black?

I'm assuming it's something I need to do while saving it, or do I need to
add something to my function to set up the image before saving it to file?

Note: I tried adding the following line (before I clip it) which I picked up
somewhere in a GDI discussion, but it doesn't help.
=> CropGraphic.Clear(Color.FromArgb(0, 0, 0, 0)), so I left it out


Public Function RoundOffImage(ByVal SrcImage As Image) As Image

Dim gpCutEllipse As New System.Drawing.Drawing2D.GraphicsPath

Dim SrcBitmap As Bitmap = New Bitmap(SrcImage)

Dim CropBitmap As New Bitmap(SrcImage.Width, SrcImage.Height,
SrcBitmap.PixelFormat)

Dim CropGraphic As Graphics

Dim SrcRect As New Rectangle(0, 0, SrcImage.Width, SrcImage.Height)

' Create region for clipping.

gpCutEllipse.AddEllipse(SrcRect)

CropGraphic = Graphics.FromImage(CropBitmap)

' Set clipping region of graphics to region.

CropGraphic.SetClip(gpCutEllipse)

CropGraphic.DrawImage(SrcImage, 0, 0)

Return CropBitmap

End Function


Can any body help me out?

Mark
 
Bitmaps don't have transparency when they are stored to disk do they, just
GIF? I don't know. But when reloading the image, couldn't you simply:

dim bmp as bitmap
bmp = new bitmap(<myimagefile.path>)
bmp.maketransparent(bmp.getpixel(0,0))


Just wondering.
 
Well that works ok if my saved images don't have any black in them (the
non-clipped areas), but some do and it's also making those pixels
transparent as well, giving me grainy images...
 
Back
Top