Export (0) Print
Expand All

HttpResponse.OutputStream Property

Enables binary output to the outgoing HTTP content body.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)

public Stream OutputStream { get; }

Property Value

Type: System.IO.Stream
An IO Stream representing the raw contents of the outgoing HTTP content body.


OutputStream is not available.

Starting with the .NET Framework version 2.0, when you use the Stream.Write(Byte[], Int32, Int32) method of the IO stream returned by the OutputStream property, the following exceptions might be thrown:

The following example calls the Save method to save a Bitmap object to the OutputStream property, and converts the image to the JPEG format. The code then calls the Dispose method on the Bitmap object and a Graphics object, releasing the resources that they were using. Finally, the code calls the Flush method to send the content of the response to the requesting client.

For a complete example, see the HttpResponse class.

<%@ Page Language="C#" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Drawing.Drawing2D" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

    private void Page_Load(object sender, EventArgs e)
        // Set the page's content type to JPEG files
        // and clears all content output from the buffer stream.
        Response.ContentType = "image/jpeg";

        // Buffer response so that page is sent
        // after processing is complete.
        Response.BufferOutput = true;

        // Create a font style.
        Font rectangleFont = new Font(
            "Arial", 10, FontStyle.Bold);

        // Create integer variables.
        int height = 100;
        int width = 200;

        // Create a random number generator and create
        // variable values based on it.
        Random r = new Random();
        int x = r.Next(75);
        int a = r.Next(155);
        int x1 = r.Next(100);

        // Create a bitmap and use it to create a
        // Graphics object.
        Bitmap bmp = new Bitmap(
            width, height, PixelFormat.Format24bppRgb);
        Graphics g = Graphics.FromImage(bmp);

        g.SmoothingMode = SmoothingMode.AntiAlias;

        // Use the Graphics object to draw three rectangles.
        g.DrawRectangle(Pens.White, 1, 1, width-3, height-3);
        g.DrawRectangle(Pens.Aquamarine, 2, 2, width-3, height-3);
        g.DrawRectangle(Pens.Black, 0, 0, width, height);

        // Use the Graphics object to write a string
        // on the rectangles.
            "ASP.NET Samples", rectangleFont,
            SystemBrushes.WindowText, new PointF(10, 40));

        // Apply color to two of the rectangles.
            new SolidBrush(
                Color.FromArgb(a, 255, 128, 255)),
            x, 20, 100, 50);

            new LinearGradientBrush(
                new Point(x, 10),
                new Point(x1 + 75, 50 + 30),
                Color.FromArgb(128, 0, 0, 128),
                Color.FromArgb(255, 255, 255, 240)),
            x1, 50, 75, 30);

        // Save the bitmap to the response stream and
        // convert it to JPEG format.
        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);

        // Release memory used by the Graphics object
        // and the bitmap.

        // Send the output to the client.

<html xmlns="http://www.w3.org/1999/xhtml" >
    <title>ASP.NET Example</title>
    <form id="form1" runat="server">

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
© 2015 Microsoft