By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,837 Members | 1,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,837 IT Pros & Developers. It's quick & easy.

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

P: n/a
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







May 3 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <ma*********@runbox.com> wrote in message
news:UY******************************@starstream.n et...
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







May 3 '06 #2

P: n/a
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...
"AMDRIT" <am****@hotmail.com> wrote in message
news:uD**************@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" <ma*********@runbox.com> wrote in message
news:UY******************************@starstream.n et...
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








May 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.