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

Image render and save problem

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


Mar 11 '06 #1
Share this Question
Share on Google+
1 Reply


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

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"Hardy Wang" <ha*******@hotmail.com> wrote in message
news:eF**************@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?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\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

Mar 11 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.