Home All Groups Group Topic Archive Search About

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

Author
3 May 2006 9:12 AM
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

Author
3 May 2006 2:09 PM
AMDRIT
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.

Show quoteHide quote
"Mark Denardo" <markdena***@runbox.com> wrote in message
news:UYCdnQAs0sdk78XZnZ2dnUVZ_sednZ2d@starstream.net...
> 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
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
Author
3 May 2006 4:20 PM
Mark Denardo
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...


Show quoteHide quote
"AMDRIT" <amd***@hotmail.com> wrote in message
news:uDXtWsrbGHA.2456@TK2MSFTNGP04.phx.gbl...
> 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.
>
> "Mark Denardo" <markdena***@runbox.com> wrote in message
> news:UYCdnQAs0sdk78XZnZ2dnUVZ_sednZ2d@starstream.net...
>> 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
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>
>