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

Working with Streams

Streams provides universal way to exchange large blocks of data. It enables you to abstract your mind from physical organization of data source or sink. Therefore you can operate with data stored at files, database, Internet, etc.

.NET Framework provides a number of classes derived from abstract class System.IO.Stream. This class provides all methods necessary to get or put data from or into the stream. You can pass a stream Load and Save methods of the Bitmap class, as well as open appropriate format reader or writer on stream.

At this topic we will demonstrate how to send an image from server to client browser. Also you can read about such application of streams as working with database, as well as loading a file from given URL.

To send any data from server to client ASP.NET provides special intrinsic object called Response. It has a property OutputStream. When you write into this stream, data is sent to client. So we should just use this property as input parameter for Save methods of the Bitmap class.

The code example below demonstrates that. Copy this code into separate file and call it, for example, WorkWithStreams1.aspx.

Visual Basic
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 100 pixels width
    bitmap.Transforms.Resize(100, 0, InterpolationMode.MediumQuality)

    'Save image to response stream
    bitmap.Save(Response.OutputStream, New JpegEncoderOptions(70, true))
End Sub
private void Page_Load(object sender, System.EventArgs e)
    Bitmap bitmap = new Bitmap(Server.MapPath(@"SourceImages/Mountain.jpg"));
    //Resize to 100 pixels width
    bitmap.Transforms.Resize(100, 0, InterpolationMode.MediumQuality);

    //Save image to response stream
    bitmap.Save(Response.OutputStream, new JpegEncoderOptions(70, true));

Quite often you should display not only one image, but also some additional HTML. But if you write any binary data into output stream of the Response, the client browser will not be able to handle HTML code sent into the same stream.

To avoid this and display additional HTML around the image we create, we need to make some additional tricks. Create a separate page which contain all necessary HTML code. At the position you need to display the image add tag <img> and set src attribute with an URL to the page which sends an image to client (i.e. WorkWithStreams1.aspx in our case).

Here is an example of this code:

	<body bgcolor="#DDDDDD">
		<img src="WorkWithStreams1.aspx">