ARTICLE

Cropping and Saving Image to Web Page Programmatically Using System.Drawing

Posted by Lajapathy Arun Articles | ASP.NET Programming April 21, 2012
In this article we are going to see how to crop and save the image to the web programmatically.
Reader Level:

In this article we are going to see how to crop and save the image to the web programmatically.

Saving Mechanism:


Step 1: The namespaces used:

using System.Drawing;
using System.Drawing.Imaging;
using System.Web;

Step 2: Usage:

protected void Page_Load(object sender, EventArgs e)
{
   SaveImageTOBrowser();
}

Step 3: Saving image to browser:

public void SaveImageTOBrowser()
{
    //Set the Local image
    Bitmap bitmap = new Bitmap(Server.MapPath("scenary.jpg"));
 
    //Get the current response
    HttpResponse response = GetHttpResponse(); 

    //Set the ContentType
    response.ContentType = "image/jpeg";
 
    //Saving Bitmap Image to
    bitmap.Save(response.OutputStream, ImageFormat.Jpeg);
}

Step 4: Getting current response:

public static HttpResponse GetHttpResponse()
{
    return HttpContext.Current.Response;
}

Step 5: Full Code Snippet:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
 
namespace SampleApplication
{
    public partial class DrawingManupulation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SaveImageTOBrowser();
        } 
        //Save Image to Browser
        public void SaveImageTOBrowser()
        {
            //Set the Local image
            Bitmap bitmap = new Bitmap(Server.MapPath("scenary.jpg"));
 
            //Get the current response
            HttpResponse response = GetHttpResponse();
 
            //Set the ContentType
            response.ContentType = "image/jpeg"
          
            //Saving Bitmap Image to
            bitmap.Save(response.OutputStream, ImageFormat.Jpeg);
        } 
        //USed to get the current response.
        public static HttpResponse GetHttpResponse()
        {
            return HttpContext.Current.Response;
        } 
    }
}

Output:

imagecrop1.gif

Cropping and Saving Mechanism

Step 1: The namespaces used:

using System.Drawing;
using System.Drawing.Imaging;
using System.Web;

Step 2: Usage:

protected void Page_Load(object sender, EventArgs e)
{
    SaveCroppedImageTOBrowser();
}

Step 3: Saving image to browser:

//CROP Image to Browser
public void SaveCroppedImageTOBrowser()
{
    //Set the Local image
    Bitmap bitmap = new Bitmap(Server.MapPath("scenary.jpg")); 
    Bitmap croppedBitmap = CropBitmap(bitmap, 0, 0, 300, 200); 
    //Get the current response
    HttpResponse response = GetHttpResponse();
 
    //Set the ContentType
    response.ContentType = "image/jpeg";
 
    //Saving Bitmap Image to
    croppedBitmap.Save(response.OutputStream, ImageFormat.Jpeg);
}

Step 4: Getting current response:

public static HttpResponse GetHttpResponse()
{
    return HttpContext.Current.Response;
}

Step 5: Full Code Snippet:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web; 
 
namespace SampleApplication
{
    public partial class DrawingManupulation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SaveCroppedImageTOBrowser();
        }
 
        //CROP Image to Browser
        public void SaveCroppedImageTOBrowser()
        {
            //Set the Local image
            Bitmap bitmap = new Bitmap(Server.MapPath("scenary.jpg")); 
            Bitmap croppedBitmap = CropBitmap(bitmap, 0, 0, 300, 200);
 
            //Get the current response
            HttpResponse response = GetHttpResponse();
 
            //Set the ContentType
            response.ContentType = "image/jpeg";
 
            //Saving Bitmap Image to
            croppedBitmap.Save(response.OutputStream, ImageFormat.Jpeg);
        }
 
        //USed to get the current response.
        public static HttpResponse GetHttpResponse()
        {
            return HttpContext.Current.Response;
        } 
        public Bitmap CropBitmap(Bitmap bitmap, int cropX, int cropY, int cropWidth, int cropHeight)
        {
            Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
            Bitmap cropped = bitmap.Clone(rect, bitmap.PixelFormat);
            return cropped;
        }
    }
}


Original Image:

 imagecrop1.gif

Cropped Image:

imagecrop2.gif
Thanks for reading this article. Have a nice day.

Login to add your contents and source code to this article
post comment
     

It's good and easy way to crop an image. Thanx for sharing..

Posted by Gaurav Gupta Apr 25, 2012

thanks luna

Posted by Lajapathy Arun Apr 24, 2012

Good article, nice and easy!

Posted by Lana Apr 23, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.