Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » WCF/Webservices »
Quick Effective Tips on Image API -- Input Validation and Format Conversion
|
Introduction While just casually playing around the System.Drawing.Imaging, I just found out a few good quick and dirty innovations. One is input validation that would be effective for websites like Dotnetspider and the other one being a website utility to achieve a quick format conversion.
Basically, the idea of input validation is that even if the user uploads an EXE file, there is a easy to catch way to thwart the attempt and throw an error to the user indicating that his file is illegal. And all it requires is about two lines of code to attempt load the image.
The next one being a quick and dirty way of converting an image from one format to another in just a jiffy.
The Power API As a quicker demonstration, only the following are required.
1) Import System.Drawing into your application. 2) Attempt to load the file using Image.FromFile API which returns an in-memory Image object.
Now, the following are achieved in a quick and easy way.
1) File Format Validation: As a typical use for websites like http://www.dotnetspider.com/, where uploading of photos is allowed as part of the profile, to thwart the attempt of the user uploading a malicious executable, this can be used. Image.FromFile would just parse the image headers and would come out with a Out of Memory message.
2) Format Conversion Utility: Once you have validated the file in step 1, the same image object can be invoked with .Save() method giving an output file name and choosing an appropriate format from the Imaging.ImageFormat enumeration.
3) Quicker Scaling Down: There is another method called GetThumbNailImage() which takes Width, Height, Call Back, IntPtr) which can achieve resizing the image dynamically. However, note that resizing the image this way might make it appear distorted. The actual decision of arriving at appropriate values for Width and Height are beyond the scope of this quick tip-note.
Quicker Demonstration Code
using System; using System.Drawing;
namespace Test { public class ImageClassTest { public static void Main(string[] args) { try { Image objImage = Image.FromFile("test.gif"); //Throws exception 'Out of memory' if file format is incorrect. Image objThumbImage = Image.GetThumbnailImage(20,20,null,IntPtr.Zero); //Dummy image of 20x20 dimensions. WARNING: This affects the image and might cause dithered image. objThumbImage.Save("test_output.jpg",System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception ImageException) { Console.WriteLine(ImageException.Message); } } } }
Summing Up
Just thought I would like to share a bit of small tip and a utility of Image.FromFile as a quick convertor and also a validation utility. As an immediate utility, I would suggest you can try using it to create FAVICON.ICO for your website using a standard logo image.
Share your tips on Imaging API as comments and let us keep this tip repository dynamic.
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|