473,756 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\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


Mar 11 '06 #1
1 4170
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 EncoderParamete rs 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*******@hotm ail.com> wrote in message
news:eF******** ******@TK2MSFTN GP14.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\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

Mar 11 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
3076
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 it using DirectX. I downloaded already the DSK etc, but I can't find how I need to draw an image on a given position. I don't need stuff to write advanced 3D-games, just painting that image. Can anybody help me with this? I'm already looking for...
3
1850
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 how to go about doing it.
2
1441
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 calculations are performed for each object to determine the position within the image. To render the image I created an aspx page that uses the following code to render the image: Response.ContentType = "image/jpeg";...
9
1487
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
1472
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 that. I am writing a WebControl Library and I want to generate everything from there... In the Render Sub of the Control I output some HTML and there I need to reference to a file in the <img> element, is there anyway to NOT have to point to a...
10
2147
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 without saving it into file on file system) Thanks
23
5937
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 on most machines, on some machines I experience this problem: When loading the page a window pops up that asks if I want to open the document show_page.asp. When I click "Open" Interdev pops up and opens up a
5
2860
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
4432
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 when iam using a normal web page... but can't in content page.... Code in Master Page <%@ Master Language="C#" AutoEventWireup="true" CodeFile="submaster.master.cs" Inherits="submaster" %> <%@ Register Assembly="AjaxControlToolkit"...
0
9462
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9287
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10046
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9857
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8723
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.