Hi ryanlcs,
Instead of Response.OutputStream.Write use BinaryWrite method of Response class.
Please refer below sample.
Namespaces
C#
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
VB.Net
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
this.DisplayMap();
}
}
private void DisplayMap()
{
string filePath = (Server.MapPath("~/Images/India Flag.png"));
Bitmap oBmp;
oBmp = new Bitmap(filePath);
byte[] bitmapBytes = ConvertToByteArray(oBmp);
Response.Clear();
Response.ContentType = "image/png";
Response.BinaryWrite(bitmapBytes);
Response.Flush();
Response.End();
}
public byte[] ConvertToByteArray(Bitmap value)
{
byte[] bitmapBytes;
using (MemoryStream stream = new MemoryStream())
{
value.Save(stream, ImageFormat.Png);
bitmapBytes = stream.ToArray();
}
return bitmapBytes;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.DisplayMap()
End If
End Sub
Private Sub DisplayMap()
Dim filePath As String = (Server.MapPath("~/Images/India Flag.png"))
Dim oBmp As Bitmap
oBmp = New Bitmap(filePath)
Dim bitmapBytes As Byte() = ConvertToByteArray(oBmp)
Response.Clear()
Response.ContentType = "image/png"
Response.BinaryWrite(bitmapBytes)
Response.Flush()
Response.End()
End Sub
Public Function ConvertToByteArray(ByVal value As Bitmap) As Byte()
Dim bitmapBytes As Byte()
Using stream As MemoryStream = New MemoryStream()
value.Save(stream, ImageFormat.Png)
bitmapBytes = stream.ToArray()
End Using
Return bitmapBytes
End Function
Screenshot