473,395 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Image render and save problem

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
1 4151
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: DraguVaso | last post by:
Hi, I have an inherited DataGrid, that does lots of extra stuff. For exemple drawing a backgroundimage in every cell. The problem is that it's taking too much time (using gdi+), so I want to do...
3
by: Shreeram | last post by:
Hello, I have a chart object which returns the chart created as a BitMap object. I want to render this Bitmap object as an image in a WebPart iam developing. Can anyone please tell me...
2
by: Jeronimo Bertran | last post by:
I have an aspx page that shows a complex image with several objects that are sensitive to mouseover events. The information about the objects in the image is obtained from a database and complex...
9
by: Lars Netzel | last post by:
Hey! Is there any possibility to render images for the web in asp.net? Is it hard? Is there something built in or do I need external commercial components? best regards /Lars
8
by: Lars Netzel | last post by:
Hey! I wrote a message yersterday and got some good answers and have now managed to generate a nice image. The problem is that it's all generated in an Images.Aspx file and I don't really want...
10
by: Mirek Endys | last post by:
Is there a posibility to make web control, that is drawing an image to webpage without needing to save it into a file? (for example, i will save picture into db server and I want to show it...
23
by: Peter | last post by:
I have a problem with a page show_image.asp that returns a jpg image under Windows XP Pro SP2. The page sets content type as: Response.ContentType = "image/jpg" While this works perfectly fine...
5
by: Tem | last post by:
I'm still trying to figure out how to do this simple task with WPF. I need to draw a black circle using WPF and generate an image file. (gif or png) Any help is greatly appreciated. Tem
3
by: premprakashbhati | last post by:
hi, good evening.. i am going to upload an image in a web form .....for that iam using HTML input(file) control and one web control button i.e., Upload_Button() here is the code ...its work fine...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.