00001: Imports System.Data.SqlClient
00002: Partial Class SQLImage
00003:     Inherits System.Web.UI.Page
00004:     Dim CN As SqlConnection, CMD As SqlCommand, DR As SqlDataReader
00005:     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
00006:         If Request.QueryString("ConnectionStringsName") = Nothing Or Request.QueryString("SelectStatement") = Nothing Then Exit Sub
00007:         Try
00008:             CN = New SqlConnection(ConfigurationManager.ConnectionStrings(Request.QueryString("ConnectionStringsName")).ConnectionString)
00009:             CN.Open()
00010:             CMD = New SqlCommand(Request.QueryString("SelectStatement"), CN)
00011:             DR = CMD.ExecuteReader
00012:             If DR.Read Then
00013:                 If IsDBNull(DR(0)) Then
00014:                     'в SQL-поле NULL
00015:                     Dim X As New Drawing.Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
00016:                     Dim G As Drawing.Graphics = Drawing.Graphics.FromImage(X)
00017:                     G.Clear(Drawing.Color.White)
00018:                     Response.ContentType = "image/bmp"
00019:                     X.Save(Response.OutputStream, Drawing.Imaging.ImageFormat.Gif)
00020:                     'так пустой рисунок почему-то не выводится
00021:                     '&H47494638396101000100800000FFFFFF00000021F90401000000002C00000000010001000002024401003B00
00022:                     'Dim OnePixel As Byte() = {&H47, &H49, &H46, &H38, &H39, &H61, &H1, &H0, &H1, &H0, &H80, &H0, &H0, &HFF, &HFF, &HFF, &H0, &H0, &H0, &H21, &HF9, &H4, &H1, &H0, &H0, &H0, &H0, &H2C, &H0, &H0, &H0, &H0, &H1, &H0, &H1, &H0, &H0, &H2, &H2, &H44, &H1, &H0, &H3B, &H0}
00023:                     'Response.BinaryWrite(OnePixel)
00024:                 Else
00025:                     'здесь все типы файлов - и возникнет исключение на неграфическом файле
00026:                     'Response.ContentType = "image/bmp"
00027:                     'Response.BinaryWrite(CType(DR(0), Byte()))
00028:                     Dim S As New System.IO.MemoryStream(CType(DR(0), Byte()))
00029:                     Dim I As Drawing.Image = Drawing.Image.FromStream(S)
00030:                     'тут главное - при выводе сохранить первоначальный формат рисунка
00031:                     Select Case I.RawFormat.Guid
00032:                         Case Drawing.Imaging.ImageFormat.Jpeg.Guid : I.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
00033:                         Case Drawing.Imaging.ImageFormat.Gif.Guid : I.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif)
00034:                         Case Drawing.Imaging.ImageFormat.Png.Guid : I.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png)
00035:                         Case Else : I.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif)
00036:                     End Select
00037:                 End If
00038:             Else
00039:                 Throw New Exception
00040:             End If
00041:             DR.Close()
00042:             CN.Close()
00043:         Catch ex As Exception
00044:             Response.ContentType = "image/bmp"
00045:             Dim X As New Drawing.Bitmap(50, 10, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
00046:             Dim G As Drawing.Graphics = Drawing.Graphics.FromImage(X)
00047:             G.Clear(Drawing.Color.White)
00048:             G.DrawString("Error", New Drawing.Font("", 10, Drawing.FontStyle.Regular, Drawing.GraphicsUnit.Pixel), Drawing.Brushes.Red, 0, -2)
00049:             X.Save(Response.OutputStream, Drawing.Imaging.ImageFormat.Gif)
00050:         End Try
00051:     End Sub
00052: End Class


Comments ( )
Link to this page: //www.vb-net.com/dotnet/tour18/11.htm
< THANKS ME>