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?i...sized11wz.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?im...sized29cy.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\P101 0001.JPG");
Image resized = RectangleDropSh adow(original, Color.Black, 10, 200);
resized.Save(@" H:\Output\resiz ed.jpg");
//resized.Save(@" H:\Output\resiz ed.jpg", ImageFormat.Jpe g);
public Image RectangleDropSh adow (Image original, Color shadowColor,
int depth, int maxOpacity) {
Bitmap tn = new Bitmap(original .Width + depth, original.Height +
depth);
Graphics tg = Graphics.FromIm age(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);
PathGradientBru sh pgb = new PathGradientBru sh(gp);
pgb.CenterColor = darkShadow;
pgb.SurroundCol ors = 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.FromIm age(patternbm);
g.FillEllipse(p gb, 0, 0, 2 * depth, 2 * depth);
g.Dispose();
pgb.Dispose();
SolidBrush sb = new SolidBrush(Colo r.FromArgb(maxO pacity,
shadowColor));
tg.FillRectangl e(sb, rc.Left + depth, rc.Top + depth, rc.Width -
(2 * depth), rc.Height - (2 * depth));
sb.Dispose();
//top left corner
tg.DrawImage(pa tternbm, new Rectangle(rc.Le ft, rc.Top, depth,
depth), 0, 0, depth, depth,
GraphicsUnit.Pi xel);
//top side
tg.DrawImage(pa tternbm, new Rectangle(rc.Le ft + depth, rc.Top,
rc.Width - (2 * depth), depth),
depth, 0, 1, depth, GraphicsUnit.Pi xel);
//top right corner
tg.DrawImage(pa tternbm, new Rectangle(rc.Ri ght - depth, rc.Top,
depth, depth),
depth, 0, depth, depth, GraphicsUnit.Pi xel);
//right side
tg.DrawImage(pa tternbm, new Rectangle(rc.Ri ght - depth, rc.Top +
depth,
depth, rc.Height - (2 * depth)), depth, depth, depth, 1,
GraphicsUnit.Pi xel);
//bottom left corner
tg.DrawImage(pa tternbm, new Rectangle(rc.Ri ght - depth,
rc.Bottom - depth, depth, depth),
depth, depth, depth, depth, GraphicsUnit.Pi xel);
//bottom side
tg.DrawImage(pa tternbm, new Rectangle(rc.Le ft + depth,
rc.Bottom - depth, rc.Width - (2 * depth), depth),
depth, depth, 1, depth, GraphicsUnit.Pi xel);
//bottom left corner
tg.DrawImage(pa tternbm, new Rectangle(rc.Le ft, rc.Bottom -
depth, depth, depth),
0, depth, depth, depth, GraphicsUnit.Pi xel);
//left side
tg.DrawImage(pa tternbm, new Rectangle(rc.Le ft, rc.Top + depth,
depth, rc.Height - (2 * depth)),
0, depth, depth, 1, GraphicsUnit.Pi xel);
patternbm.Dispo se();
tg.DrawImage(or iginal, new Rectangle(0, 0, original.Width,
original.Height ),
0, 0, original.Width, original.Height , GraphicsUnit.Pi xel);
return (Image)tn;
}
Regards
Hardy