Image Cropping by WPF: Using System.Windows.Media.Imaging;

Introducing System.Windows.Media.Imaging namespace provides types that are used to encode and decode bitmap images.


Here I am going to explain a sample application which is used to cropping images. Imaging namespace is in under Windows Presentation Foundation will do the image manipulation in simple manner.


First of all, we need to add the following reference in our project.

- PresentationCore

- PresentationFramework

- WindowsBase


Then use the following namespace,


using System.Windows.Media;

using System.Windows.Media.Imaging;


The method for creating crop images with passing appropriate parameters as follows,


//Method for Creating Crop Image

private void CreateCropImage(int x, int y, int width, int height, string SourceImagePath, string TargetImagePath, string ImageType)

{

byte[] imgBytest = LoadImageData(SourceImagePath);

System.Drawing.Image img=new Bitmap(SourceImagePath);

CroppedBitmap cb = new CroppedBitmap((BitmapSource)CreateImage(imgBytest,img.Width,img.Height), new Int32Rect(x, y, width, height));

ImageSource imageSource = cb;

byte[] imageBytes = GetEncodedImageData(imageSource, ImageType);

SaveImageData(imageBytes, TargetImagePath);

}


//Method for getting image bytes from image's source path by using FileStream.

private static byte[] LoadImageData(string filePath)

{

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);

BinaryReader br = new BinaryReader(fs);

byte[] imageBytes = br.ReadBytes((int)fs.Length);

br.Close();

fs.Close();

return imageBytes;

}

//Method for getting cropped image.

private static ImageSource CreateImage(byte[] imageData, int decodePixelWidth, int decodePixelHeight)

{

if (imageData == null) return null;

BitmapImage result = new BitmapImage();

result.BeginInit();

if (decodePixelWidth > 0)

{

result.DecodePixelWidth = decodePixelWidth;

}

if (decodePixelHeight > 0)

{

result.DecodePixelHeight = decodePixelHeight;

}

result.StreamSource = new MemoryStream(imageData);

result.CreateOptions = BitmapCreateOptions.None;

result.CacheOption = BitmapCacheOption.OnLoad;

result.EndInit();

return result;

}


//Method for getting encoded image data for the cropped image.

internal byte[] GetEncodedImageData(ImageSource image, string preferredFormat)

{

byte[] returnData = null;

BitmapEncoder encoder = null;

switch (preferredFormat.ToLower())

{

case ".jpg":

case ".jpeg":

encoder = new JpegBitmapEncoder();

break;

case ".bmp":

encoder = new BmpBitmapEncoder();

break;

case ".png":

encoder = new PngBitmapEncoder();

break;

case ".tif":

case ".tiff":

encoder = new TiffBitmapEncoder();

break;

case ".gif":

encoder = new GifBitmapEncoder();

break;

case ".wmp":

encoder = new WmpBitmapEncoder();

break;

}

if (image is BitmapSource)

{

MemoryStream stream = new MemoryStream();

encoder.Frames.Add(BitmapFrame.Create(image as BitmapSource));

encoder.Save(stream);

stream.Seek(0, SeekOrigin.Begin);

returnData = new byte[stream.Length];

BinaryReader br = new BinaryReader(stream);

br.Read(returnData, 0, (int)stream.Length);

br.Close();

stream.Close();

}

return returnData;

}

//Method for saving the image data as image in to spec

private static void SaveImageData(byte[] imageData, string filePath)

{

FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);

BinaryWriter bw = new BinaryWriter(fs);

bw.Write(imageData);

bw.Close();

fs.Close();

}


That's it...


...S.VinothkumaR.

4 comments:

Siten said...

ok cool thanks for that, however Where do i put the C# code you typed? do I put all code, do i create a button called crop, and then put this code in there, can you explain where i put the code so it can help me?

Thanks

Unknown said...

Thanks you very much !!!
Perfect !

Unknown said...

Thanx for this Help!!Please tell me where should I Place this Code..I mean On which event Handler

Vidya said...

Please tell me where to call these method for croping image. Please send me sample using this code.