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:
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:
Cropped Image:
Thanks for reading this article. Have a nice day.