Bitmap Extension Methods; for Resize, Crop and save

by Pieter Brinkman 24. July 2008 08:11

For Bitmap manipulation I wrote three extension methods. With this method you can resize, crop and save (as jpg) your image.

 

Resize Method

public static Bitmap resizeImage(this Bitmap input, Size size)

{

int sourceWidth = input.Width;

int sourceHeight = input.Height;

float nPercent = 0;

float nPercentW = 0;

float nPercentH = 0;

nPercentW = ((float)size.Width / (float)sourceWidth);nPercentH = ((float)size.Height / (float)sourceHeight);

if (nPercentH < nPercentW)

nPercent = nPercentH;

else

nPercent = nPercentW;

int destWidth = (int)(sourceWidth * nPercent);

int destHeight = (int)(sourceHeight * nPercent);

Bitmap b = new Bitmap(destWidth, destHeight);

Graphics g = Graphics.FromImage(b);g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.DrawImage(input, 0, 0, destWidth, destHeight);

g.Dispose();

return b;

}

 

Crop Method

public static Bitmap cropImage(this Bitmap input, Rectangle cropArea)

{

Bitmap bmpImage = new Bitmap(input);Bitmap bmpCrop = bmpImage.Clone(cropArea, input.PixelFormat);

return (System.Drawing.Bitmap)(bmpCrop);

}

 

Safe Method

public static void saveJpeg(this Bitmap img, string path, long quality)

{

// Encoder parameter for image quality

EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);

// Jpeg image codec

ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg"); if (jpegCodec == null)

return;

EncoderParameters encoderParams = new EncoderParameters(1);

encoderParams.Param[0] = qualityParam;

img.Save(path, jpegCodec, encoderParams);

}

public static ImageCodecInfo getEncoderInfo(string mimeType)

{

// Get image codecs for all image formats

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

// Find the correct image codec

for (int i = 0; i < codecs.Length; i++)

if (codecs[i].MimeType == mimeType)

return codecs[i]; return null;

}

Tags: , , , , , ,

ASP.Net

String Extension Method: IsGuid()

by Pieter Brinkman 23. June 2008 08:46

I wrote an extension method for the string type. With this method you can check if an string is an Guid. The method returns a Bool.

public static class StringExtensions
{
  public static bool IsGuid(this string input)
  {
    Regex isGuid = new Regex(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled);
    bool isValid = false;
    if (input != null)
    {
      if (isGuid.IsMatch(input))
      {
        isValid = true;
      }
    }
  return isValid;
  }
}

If you're wondering I didn't wrote the Regex myself :-) (but it works)

This brute-force method is mutch faster (thanks to Arjan and Tag for the comments):

public static bool IsGuid(this string input)
{
try
{
new Guid(input);
return true;
}
catch (ArgumentNullException) {}
catch (FormatException) {}
catch (OverflowException) {}
return false;
}

 

A good example how extension methods can make some things easier. 

Hope it helps!

Tags: , ,

ASP.Net

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About Me

My name is Pieter Brinkman I am a .NET Software Engineer for Achmea IT in De Meern, The Netherlands. My interests are mainly web applications created with ASP.NET, MSSQL and Silverlight.

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

RecentComments

Comment RSS

Most comments

club penguin cheats club penguin cheats
4 comments
us United States
Web Design Company Web Design Company
2 comments
Web design Web design
2 comments
gb United Kingdom