Working with Streams

Streams provide a universal way to exchange large blocks of data between the different modules of a program. They allow you to abstract your code from the physical organization of data sources and sinks, so you can process data stored in files, databases, the Internet, etc., using one interface.

.NET Framework provides a number of classes derived from the abstract System.IO.Stream class. To load an image from a stream you can use an appropriate format reader or the Bitmap.#ctor(Stream) constructor. Similarly, to save an image to a stream, use an appropriate format writer or the Bitmap.Save(Stream, WriterSettings) method.

This topic describes how to send an image from a server to a client browser. The Working with Databases and Working with URLs topics explain using streams with databases and URLs.

To send any data from a server to a client ASP.NET provides a special intrinsic Response object. Its OutputStream property allows the sending of data to a client when you write anything into this stream. Use this property as an input parameter for a format writer or the Bitmap.Save(Stream, WriterSettings) method.

The following code reads JPEG image, resizes it, and writes the result to the response output stream. The code uses pipelines; if you are not familiar with pipelines, read the Understanding Image Processing Approaches in Graphics Mill topic.

C#
protected void Page_Load(object sender, EventArgs e)
{
    using (var reader = Aurigma.GraphicsMill.Codecs.ImageReader.Create(Server.MapPath(@"Images/in.jpg")))
    using (var resize = new Aurigma.GraphicsMill.Transforms.Resize(100, 0, Aurigma.GraphicsMill.Transforms.ResizeInterpolationMode.High))
    using (var writer = new Aurigma.GraphicsMill.Codecs.JpegWriter(Response.OutputStream))
    {
        Response.Clear();
        Aurigma.GraphicsMill.Pipeline.Run(reader + resize + writer);
        Response.End();
    }
}

Usually an image should be displayed along with some additional HTML. And if you write binary data into the response output stream, a client browser cannot handle HTML code sent into the same stream. So, you need a workaround to avoid this and display the resulting image and additional HTML on one page. To perform this action, create a separate page containing all necessary HTML code. At the position you want to display the image add the <img> tag and set its src attribute to the page containing the previous code snippet (let us name it JpegImage.aspx).

The following code inserts the resulting image to an HTML page:

ASP.NET
<html>
    <head runat="server"></head>
    <body bgcolor="#DDDDDD">
        <img src="JpegImage.aspx" />
    </body>
</html>

See Also

Reference

Manual