NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
DocuTerminal
How Long For Me
My Music To Me
SaveMySerials
TypeCount

Blog
Twitter

NerdyHearn - Blog


<< Back To All Blogs

Creating High Quality Images with C# and GDI

Thursday, August 13th, 2009

I have posted in the past about creating organizational charts using C# and GDI, but I have recently come across a scenario in which I needed to make the charts show up as 100% quality (they were blurred because of the default, lower-quality GDI output).

Low and behold GDI does indeed support the ability to change output quality to a Bitmap, and while it isn't the easiest thing to figure out originally, it really isn't that bad.

So I figure the best way is to jump right into the code, so here we go. All of these classes are available in the System.Drawing.Imaging namespace of .NET:

// Encoder parameters can hold multiple parameters to pass to the Save method for the Bitmap class
EncoderParameters encoderParams = new EncoderParameters();

// Requires an array for the quality parameter
long[] quality = new long[1];

// 100 is 100%, 0 is 0%
quality[0] = 100;

// Create a new encoder parameter with the specified quality array from above
EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality);

// Add this parameter to the entire collection
encoderParams.Param[0] = qualityParam;

// This could either be a newly-created Bitmap, or come from a loaded image, etc
Bitmap result = GetBitmapOutput();

// We can no longer pass the normal ImageFormat Enum to the Save method, so we need to find the actual encoder from C#/GDI
ImageCodecInfo[] ici = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo encoder = null;

// Loop through them all and set the encoder if we find it to be on JPEG, this could also be used for PNG, GIF, etc
for (int a = 0; a < ici.Length; a++)
{
if (ici[a].FormatDescription.Equals("JPEG"))
{
encoder = ici[a];
}
}

// Save the Bitmap to the output stream (in my case), with the specified encoder and encoder parameters
result.Save(Response.OutputStream, encoder, encoderParams);

// Clean up
result.Dispose();

Not too bad, hopefully that will help some of you along the way.

Imagin' Tom Out.

Tags

CSharp Howto

Related Blogs

Bitmasking userAccountControl attribute in LDAP from C#
Code Analysis with NDepend V3
Writing console output from a Windows form in C#
Copy a DLL from the GAC
Querying Table Entities with Microsoft Azure (And ADO.NET Data Services Framework)

Comments

Currently no comments.

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:



Enter the text above, except for the 1st and last character:


NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. DocuTerminal - Online Filing Cabinet solution. Scan, search and archive your paper documents. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers My Music To Me - Stream your subsonic music collection to your Sonos wireless system TypeCount - Count how often you type across different computers! ServrTrackr - Monitor your SSL certificates, website uptime, and DNS configurations