Re: Cropping or extracting ellipse from image



JPEG images only save with 24 bits per pixel. This means no alpha channel
and hence no transparency.

The only way out is to save as GIF which brings up the issue of palette
optimization or as PNG which brings up the issue of compatibility.

Really I find that the small incidence of incompatibility with PNG is
negligable in comparison with the joys of GIF optimization.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.



<brett.raven@xxxxxxxxx> wrote in message
news:1144756096.443526.262310@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi Bob,
I used your code, which definitely does crop the picture with an
ellipse (thanks again!), however when I save the image as a JPEG, the
background that should be transparent is black.

Here is my code:

pics.Bitmap bm = (pics.Bitmap)pics.Bitmap.FromFile(path +
filename);
pics.Bitmap bt = new pics.Bitmap(bm.Width, bm.Height);
pics.Graphics g = pics.Graphics.FromImage(bt);
GraphicsPath gp=new GraphicsPath();
gp.AddEllipse(10,10,bm.Width-20,bm.Height-20);
g.Clear(pics.Color.Magenta);
g.SetClip(gp);
g.DrawImage(bm,new
pics.Rectangle(0, 0, bm.Width, bm.Height), 0, 0, bm.Width,
bm.Height, pics.GraphicsUnit.Pixel);
g.Dispose();
bt.MakeTransparent(pics.Color.Magenta);

bt.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);

-----------------------------
I take this image and merge it with a couple others using the following
code - and it shows up on the image with a black square background ...
-----------------------------

System.Drawing.Graphics myGraphic = null;

pics.Image m = pics.Image.FromFile(imgPath +
"imgBlank.jpg");
myGraphic = System.Drawing.Graphics.FromImage(m); // this
is the blank background

pics.Image imgInvite;
imgInvite = pics.Image.FromFile(imgPath + "frame_pic.jpg");
// this is the frame

pics.Image imgPhoto;
imgPhoto = pics.Image.FromFile(imgSelectedPhoto); // this
is the user photo
myGraphic.DrawImage(imgInvite, 0, 0, 500, 311);
myGraphic.DrawImage(imgPhoto, 20, 20, 100, 100);
myGraphic.Save();
m.Save(mergedFileName,
System.Drawing.Imaging.ImageFormat.Jpeg);


It just seems like the transparency isn't working. Is this a problem
for Jpegs via GDI?

Cheers and thanks again.
Brett



.



Relevant Pages

  • Re: What about JPEG, GIF, etc.!!
    ... The patent on GIF expired about two years ago. ... I think GDI+ supports both JPEG and GIF natively. ... Regards, ...
    (microsoft.public.vc.mfc)
  • Re: What about JPEG, GIF, etc.!!
    ... The patent on GIF expired about two years ago. ... I think GDI+ supports both JPEG and GIF natively. ... Regards, ...
    (microsoft.public.vc.language)
  • Re: Drawing Text onto Image
    ... You can definitely get better image quality from GIF than from a JPEG of the ... The problem arises however when GIF is saved from GDI+ using the standard ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Drawing Text onto Image
    ... I have chosen to use Jpeg with no compression. ... Public Shared Function GetCodecInfo(ByVal mimeType As String) As ... > the same file-size because GIF uses lossless compression whereas JPEG is ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Humans "unique" social
    ... > advantage of JPEG over GIF?". ... image into colored pixels before compressing the image." ...
    (sci.bio.evolution)

Loading