Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

Image render and save problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hardy Wang - 11 Mar 2006 02:15 GMT
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
Kevin Spencer - 11 Mar 2006 13:49 GMT
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.

Signature

HTH,

Kevin Spencer
Microsoft MVP
.Net Developer

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

> Hi,
>    I found a piece of code to add drop shadow to a photo like below, after
[quoted text clipped - 104 lines]
> Regards
> Hardy

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.