|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Image render and save problemI found a piece of code to add drop shadow to a photo like below, after I save the image, it is actually a BMP file even though I specify a JPG file extension (see http://img140.imageshack.us/my.php?image=resized11wz.jpg). If I force to save in JPG format (see the commented line), then the whole shadow is a messup (see http://img70.imageshack.us/my.php?image=resized29cy.jpg). I am just wandering are there any attributes / formats not supported by JPG format? Did I do something wrong? Image original = Image.FromFile(@"H:\Input\P1010001.JPG"); Image resized = RectangleDropShadow(original, Color.Black, 10, 200); resized.Save(@"H:\Output\resized.jpg"); //resized.Save(@"H:\Output\resized.jpg", ImageFormat.Jpeg); public Image RectangleDropShadow (Image original, Color shadowColor, int depth, int maxOpacity) { Bitmap tn = new Bitmap(original.Width + depth, original.Height + depth); Graphics tg = Graphics.FromImage(tn); Rectangle rc = new Rectangle(0, 0, original.Width + depth, original.Height + depth); //calculate the opacities Color darkShadow = Color.FromArgb(maxOpacity, shadowColor); Color lightShadow = Color.FromArgb(0, shadowColor); //Create a brush that will create a softshadow circle GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(0, 0, 2 * depth, 2 * depth); PathGradientBrush pgb = new PathGradientBrush(gp); pgb.CenterColor = darkShadow; pgb.SurroundColors = new Color[] { lightShadow }; //generate a softshadow pattern that can be used to paint the shadow Bitmap patternbm = new Bitmap(2 * depth, 2 * depth); Graphics g = Graphics.FromImage(patternbm); g.FillEllipse(pgb, 0, 0, 2 * depth, 2 * depth); g.Dispose(); pgb.Dispose(); SolidBrush sb = new SolidBrush(Color.FromArgb(maxOpacity, shadowColor)); tg.FillRectangle(sb, rc.Left + depth, rc.Top + depth, rc.Width - (2 * depth), rc.Height - (2 * depth)); sb.Dispose(); //top left corner tg.DrawImage(patternbm, new Rectangle(rc.Left, rc.Top, depth, depth), 0, 0, depth, depth, GraphicsUnit.Pixel); //top side tg.DrawImage(patternbm, new Rectangle(rc.Left + depth, rc.Top, rc.Width - (2 * depth), depth), depth, 0, 1, depth, GraphicsUnit.Pixel); //top right corner tg.DrawImage(patternbm, new Rectangle(rc.Right - depth, rc.Top, depth, depth), depth, 0, depth, depth, GraphicsUnit.Pixel); //right side tg.DrawImage(patternbm, new Rectangle(rc.Right - depth, rc.Top + depth, depth, rc.Height - (2 * depth)), depth, depth, depth, 1, GraphicsUnit.Pixel); //bottom left corner tg.DrawImage(patternbm, new Rectangle(rc.Right - depth, rc.Bottom - depth, depth, depth), depth, depth, depth, depth, GraphicsUnit.Pixel); //bottom side tg.DrawImage(patternbm, new Rectangle(rc.Left + depth, rc.Bottom - depth, rc.Width - (2 * depth), depth), depth, depth, 1, depth, GraphicsUnit.Pixel); //bottom left corner tg.DrawImage(patternbm, new Rectangle(rc.Left, rc.Bottom - depth, depth, depth), 0, depth, depth, depth, GraphicsUnit.Pixel); //left side tg.DrawImage(patternbm, new Rectangle(rc.Left, rc.Top + depth, depth, rc.Height - (2 * depth)), 0, depth, depth, 1, GraphicsUnit.Pixel); patternbm.Dispose(); tg.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel); return (Image)tn; } Regards Hardy Hi Hardy,
JPGs are a compressed format. You didn't specify the compression or quality of the image. Your best bet in terms of quality will be to use the overload which takes an ImageCodecInfo and EncoderParameters to save the image in a specific format. This gives you total control over the image file format. -- Show quoteHTH, Kevin Spencer Microsoft MVP ..Net Developer Presuming that God is "only an idea" - Ideas exist. Therefore, God exists. "Hardy Wang" <hardyw***@hotmail.com> wrote in message news:eFY1GGLRGHA.4792@TK2MSFTNGP14.phx.gbl... > Hi, > I found a piece of code to add drop shadow to a photo like below, after > I save the image, it is actually a BMP file even though I specify a JPG > file extension (see > http://img140.imageshack.us/my.php?image=resized11wz.jpg). If I force to > save in JPG format (see the commented line), then the whole shadow is a > messup (see http://img70.imageshack.us/my.php?image=resized29cy.jpg). > > I am just wandering are there any attributes / formats not supported by > JPG format? > > Did I do something wrong? > > Image original = Image.FromFile(@"H:\Input\P1010001.JPG"); > Image resized = RectangleDropShadow(original, Color.Black, 10, 200); > resized.Save(@"H:\Output\resized.jpg"); > //resized.Save(@"H:\Output\resized.jpg", ImageFormat.Jpeg); > > public Image RectangleDropShadow (Image original, Color > shadowColor, > int depth, int maxOpacity) { > Bitmap tn = new Bitmap(original.Width + depth, original.Height > + > depth); > Graphics tg = Graphics.FromImage(tn); > > Rectangle rc = new Rectangle(0, 0, original.Width + depth, > original.Height + depth); > > //calculate the opacities > Color darkShadow = Color.FromArgb(maxOpacity, shadowColor); > Color lightShadow = Color.FromArgb(0, shadowColor); > > //Create a brush that will create a softshadow circle > GraphicsPath gp = new GraphicsPath(); > gp.AddEllipse(0, 0, 2 * depth, 2 * depth); > PathGradientBrush pgb = new PathGradientBrush(gp); > pgb.CenterColor = darkShadow; > pgb.SurroundColors = new Color[] { lightShadow }; > > //generate a softshadow pattern that can be used to paint the > shadow > Bitmap patternbm = new Bitmap(2 * depth, 2 * depth); > Graphics g = Graphics.FromImage(patternbm); > g.FillEllipse(pgb, 0, 0, 2 * depth, 2 * depth); > g.Dispose(); > pgb.Dispose(); > > SolidBrush sb = new SolidBrush(Color.FromArgb(maxOpacity, > shadowColor)); > tg.FillRectangle(sb, rc.Left + depth, rc.Top + depth, > rc.Width - > (2 * depth), rc.Height - (2 * depth)); > sb.Dispose(); > > //top left corner > tg.DrawImage(patternbm, new Rectangle(rc.Left, rc.Top, depth, > depth), 0, 0, depth, depth, > GraphicsUnit.Pixel); > > //top side > tg.DrawImage(patternbm, new Rectangle(rc.Left + depth, rc.Top, > rc.Width - (2 * depth), depth), > depth, 0, 1, depth, GraphicsUnit.Pixel); > > //top right corner > tg.DrawImage(patternbm, new Rectangle(rc.Right - depth, rc.Top, > depth, depth), > depth, 0, depth, depth, GraphicsUnit.Pixel); > > //right side > tg.DrawImage(patternbm, new Rectangle(rc.Right - depth, rc.Top > + > depth, > depth, rc.Height - (2 * depth)), depth, depth, depth, 1, > GraphicsUnit.Pixel); > > //bottom left corner > tg.DrawImage(patternbm, new Rectangle(rc.Right - depth, > rc.Bottom - depth, depth, depth), > depth, depth, depth, depth, GraphicsUnit.Pixel); > > //bottom side > tg.DrawImage(patternbm, new Rectangle(rc.Left + depth, > rc.Bottom - depth, rc.Width - (2 * depth), depth), > depth, depth, 1, depth, GraphicsUnit.Pixel); > > //bottom left corner > tg.DrawImage(patternbm, new Rectangle(rc.Left, rc.Bottom - > depth, depth, depth), > 0, depth, depth, depth, GraphicsUnit.Pixel); > > //left side > tg.DrawImage(patternbm, new Rectangle(rc.Left, rc.Top + depth, > depth, rc.Height - (2 * depth)), > 0, depth, depth, 1, GraphicsUnit.Pixel); > > patternbm.Dispose(); > > tg.DrawImage(original, new Rectangle(0, 0, original.Width, > original.Height), > 0, 0, original.Width, original.Height, GraphicsUnit.Pixel); > > return (Image)tn; > } > > Regards > Hardy > > > > |
|||||||||||||||||||||||