Image Processing


The last thing I want to do in this chapter is introduce you to image processing in ASP.NET. I have created an application that loads in an image, then darkens the image, then saves it out to disk, and then displays it in the HTML page. I do all this in the Page_Load() method so that the darkened image is prepared before the HTML code is actually sent back to the client. You can see the application running in Figure 16.5. There is not actually any interaction with a user ; the page simply loads, does the conversion, and displays both images.

Figure 16.5. You can see that the second image has been darkened.


Look at the code in Listing 16.7, which does the image processing. The first thing that happens in the Page_Load() method is that the Boats.JPG file is loaded into a Bitmap object. Then, the width and height are obtained and stored in locally declared integer variables .

A new bitmap is created that will hold the darkened image data. Then, each pixel of the original image is obtained with the GetPixel() method. A Color object is returned from the GetPixel() method.

The Color object has three properties that this code uses to retrieve the red, blue, and green color components for each pixel. The properties are simply R , G , and B . Their values range from 0 to 255, which is the standard range of RGB values for images.

After the color information is retrieved from each pixel, a value of 90 is subtracted from the red, the green, and the blue component. Of course, it is essential to ensure that none of these values goes below 0. There can't be a negative color component value. After the darkened red, blue, and green data is ready, a new Color object is created with the FromARGB() method. With this newly created Color object the SetPixel method is called for the darkened Bitmap object. The last thing that happens in this PageLoad() method is that the darkened image is saved to disk. This enables the HTML code then to load the darkened image and display it.

Listing 16.7 Code That Darkens an Image, Then Saves It to the Server's Hard Drive
 private void Page_Load(object sender, System.EventArgs e)  {    Bitmap original = new Bitmap( Request.MapPath( "boats.jpg" ) );    int nWidth = original.Width;    int nHeight = original.Height;    Bitmap darkened = new Bitmap( nWidth, nHeight, PixelFormat.Format24bppRgb );    for( int y=0; y<nHeight; y++ )    {      for( int x=0; x<nWidth; x++ )      {        Color col = original.GetPixel( x, y );        int R = col.R - 90;        if( R < 0 )        {          R = 0;         }        int G = col.G - 90;        if( G < 0 )        {          G = 0;        }        int B = col.B - 90;        if( B < 0 )        {          B = 0;        }        Color newCol = Color.FromArgb( R, G, B );        darkened.SetPixel( x, y, newCol );      }    }    darkened.Save( Request.MapPath( "darkened.jpg" ), ImageFormat.Jpeg );  } 

Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233 © 2008-2017.
If you may any questions please contact us: