This documentation is for the old version. Go to the latest Graphics Mill docs

Image Processing in ASP.NET

It is not always necessary to use the web control to operate with images in the ASP.NET application. For example, if you are processing multiple files and need not to display them, or if you want just send a thumbnail to browser (without rich features of the web control), you may prefer to use core class library of the Graphics Mill for .NET instead of the web control. This topic describes how to do it.

You can write ASP.NET application in two ways: using .aspx files only and using CodeFile classes (by arranging the code in .aspx.cs or .aspx.vb files). There is no difference between these methods from performance point of view, so use the one which is more convenient to you.

Using Graphics Mill for .NET Without CodeFile

If you do not use CodeFile at your web application and prefer using .aspx files only, you can use the code example below. It loads the file, resizes it, and sends it to client browser.

ASP.NET Visual Basic
<%@ Page Language="vb" AutoEventWireup="True"%>
<%@ Import Namespace="Aurigma.GraphicsMill"%>
<%@ Import Namespace="Aurigma.GraphicsMill.Codecs"%>
<%@ Import Namespace="Aurigma.GraphicsMill.Transforms"%>
<script language="vb" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
	Dim bitmap As New Bitmap(Server.MapPath("SourceImages/Mountain.jpg"))
	
	'Resize to 80 pixels width 
	bitmap.Transforms.Resize(80, 0, InterpolationMode.MediumQuality)

	'Specify that we are going to save to JPEG
	Dim encoderOptions As New JpegEncoderOptions

	'Save image to response stream
	Response.Clear()
	
	Response.ContentType="image/jpeg"
	
	bitmap.Save(Response.OutputStream, encoderOptions)
	
	Response.End()
End Sub
</script>
ASP.NET C#
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="Aurigma.GraphicsMill"%>
<%@ Import Namespace="Aurigma.GraphicsMill.Codecs"%>
<%@ Import Namespace="Aurigma.GraphicsMill.Transforms"%>
<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
	Bitmap bitmap = new Bitmap(Server.MapPath("SourceImages/Mountain.jpg"));
	
	//Resize to 80 pixels width 
	bitmap.Transforms.Resize(80, 0, InterpolationMode.MediumQuality);

	//Specify that we are going to save to JPEG
	JpegEncoderOptions encoderOptions = new JpegEncoderOptions();

	//Save image to response stream
	Response.Clear();
	
	Response.ContentType="image/jpeg";
	
	bitmap.Save(Response.OutputStream, encoderOptions);
	
	Response.End();
}
</script>

Let's examine more detailed how the image is displayed at the browser.

To send any piece of data (HTML code, images, or whatever else data) from server to client, we should use the intrinsic ASP.NET object Response. It has a property called OutputStream. When you write data to this stream, it is sent to the client browser.

In the code example we pass this stream into the Save method of the Bitmap and the image is written into the stream. The browser recognizes it as an image (you make a hint to the browser by specifying a content type) and displays it. However it means that you cannot write additional data into the same stream (such as an outer HTML code, etc.). The browser will decide that this additional data is a part of the image binaries. Most likely user will see it as a corrupted image.

But fortunately there is a workaround for this problem. For example, if you want to add outer HTML code, do the following:

  1. Create a separate page which will contain necessary HTML code.
  2. In the location where you want to put the image add the <img> tag and specify the URL to the .aspx page which produces necessary image.

For example, let's assume that the page which creates the image (e.g. above code example) is called ImageProcessing.aspx. Create new page called ShowImage.htm in the same virtual folder with the ImageProcessing.aspx, and add the following code:

HTML
<html>
  <body>
    <p>The comment to the image.</p>
    <p><img src="ImageProcessing.aspx"></p>
  </body>
</html>

Using Graphics Mill for .NET with CodeFile

Now let's examine the code example, which works in the same way, but which is written using the CodeBehind classes.

This code example consists of two files: ImageProcessing2.aspx and ImageProcessing2.aspx.cs (or ImageProcessing2.aspx.vb if you are writing on VB.NET).

The first file, ImageProcessing2.aspx, contains the directives necessary so that ASP.NET engine could find the implementation of the CodeFile classes:

ASP.NET Visual Basic
<%@ Page Language="vb" AutoEventWireup="false" Inherits="GraphicsMillVB.ImageProcessing2"
	CodeFile="ImageProcessing2.aspx.vb" %>

ASP.NET C#
<%@ Page Language="c#" AutoEventWireup="true" Inherits="GraphicsMillCS.ImageProcessing2"
	CodeFile="ImageProcessing2.aspx.cs" %>

The actual code is placed in the ImageProcessing2.aspx.cs (or ImageProcessing2.aspx.vb) file:

Visual Basic
Imports Aurigma.GraphicsMill
Imports Aurigma.GraphicsMill.Codecs
Imports Aurigma.GraphicsMill.Transforms

Namespace GraphicsMillVB

    Partial Class ImageProcessing2
        Inherits System.Web.UI.Page

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            Dim bitmap As New Bitmap(Server.MapPath("SourceImages/Mountain.jpg"))

            'Resize to 80 pixels width 
            bitmap.Transforms.Resize(80, 0, InterpolationMode.MediumQuality)

            'Specify that we are going to save to JPEG
            Dim encoderOptions As New JpegEncoderOptions

            'Save image to response stream
            Response.Clear()

            Response.ContentType = "image/jpeg"

            bitmap.Save(Response.OutputStream, encoderOptions)

            Response.End()
        End Sub

    End Class

End Namespace
C#
using System;
using Aurigma.GraphicsMill;
using Aurigma.GraphicsMill.Codecs;
using Aurigma.GraphicsMill.Transforms;

namespace GraphicsMillCS
{
	public partial class ImageProcessing2 : System.Web.UI.Page
	{
		protected void Page_Load(object sender, System.EventArgs e)
		{
			Aurigma.GraphicsMill.Bitmap bitmap = 
				new Aurigma.GraphicsMill.Bitmap(Server.MapPath("SourceImages/Mountain.jpg"));

			//Resize to 80 pixels width 
			bitmap.Transforms.Resize(80, 0, InterpolationMode.MediumQuality);

			//Specify that we are going to save to JPEG
			JpegEncoderOptions encoderOptions = new JpegEncoderOptions();

			//Save image to response stream
			Response.Clear();

			Response.ContentType="image/jpeg";

			bitmap.Save(Response.OutputStream, encoderOptions);

			Response.End();
		}

	}
}
Note

You cannot send HTML code or put it in the ImageProcessing2.aspx file for the same reasons as for the first code example. If you need to add outer HTML, use the same technique as described above.