Finding JPG image dimensions programmatically using ASP.NET/C#
.NET includes everything necessary to programmatically read the dimensions of image files such as .JPGs. This is useful in cases where you want to explicitly specify the size of an image at runtime. In my case, I had an image cropping control that required I send the dimensions of the image to be cropped. The means to return image size in .NET is a bit verbose, but it is nice that you can do this without going to an external component or anything outside of what's baked into .NET. See below.
using System.Drawing;
using System.Text;
ImageCropper1.ImageUrl = "../../test8180-uweek mail prelim design.jpg";
string myDimensions = ImageProcessing.GetImageDimensions(@"c:\Inetpub\wwwroot\ni\test8180-uweek mail prelim design.jpg");
string[] dimensions = myDimensions.Split(new char[] {','});
string imageWidth = Convert.ToString(dimensions[0]);
string imageHeight = Convert.ToString(dimensions[1]);
ImageCropper1.Width = Convert.ToInt16(imageWidth);
ImageCropper1.Height = Convert.ToInt16(imageHeight);
Imageprocessing is a custom class I wrote/stole: something old, something new, something borrowed, and something blue. It consists mainly of static methods, including the GetImageDimensions function called above:
public static string GetImageDimensions(string fileNameAndPath)
{
System.Drawing.Image theImage = System.Drawing.Image.FromFile(fileNameAndPath);
float UploadedImageWidth = theImage.PhysicalDimension.Width;
float UploadedImageHeight = theImage.PhysicalDimension.Height;
string size = UploadedImageWidth.ToString() + "," + UploadedImageHeight.ToString();
return size;
}
Two things bug me. The first: the Krispware cropping control demands the following structure for the path to the image, at least on my local development machine:
ImageCropper1.ImageUrl = "../../test8180-uweek mail prelim design.jpg";In my case and on my machine, I found I could not use the typical "~/" pathing notation to the root; on the same page I placed an Image control and was able to use "~/" just fine. This could be caused by any of a number of things:
a) an inherent limit of the control
b) something funky that's specific to my dev machine, or
c) something funky with how the virtual directories were set up on installation, which I don't really understand well. (Any help out there?)
Second question. In the course of hacking around with this started to think, "wow, maybe I should write my own augmented image class that would bundle in loads of functionality and allow capabilties like:
KensCoolImageClass CoolImage = new KensCoolImageClass();
Coolimage.RenderAsCropControl();
Coolimage.RenderAsUserControl(300,200,"isZoomable" ...);
Coolimage.SendToFriend(mediumSize, "kenfine@u.washington.edu);
Coolimage.CropByCoords(22, 200,4, 400);
Coolimage.UpdateExifData("metadata1","metadata2,...);
Coolimage.PostToFlickr(encrypedAccountName, encrypedAccountPassword);
Coolimage.ResizeByPercentage(40);
Coolimage.Rotate(90, "clockwise");
int theHeight = Coolimage.Height;
int theWidth = Coolimage.Width;
I'm curious why some of these functionalities are not built into the framework itself: my code above works, but it seems a little more painful than should be necessary.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home