Rendering Access Embedded OLE images into SQL back into JPEG images - AppGeo/GPV GitHub Wiki
We had a existing database of BLOB images in SQL for Sewer Cards. These were unformuately were inputted using TIFFs and the embedded OLE object method from Microsoft Access. As a result it was a two step process to render them for the web as JPEG. It only requires two files: ScanViewImage.aspx ScanViewImage.aspx.cs
The configurable pieces used are in ScanViewImage.aspx.cs:
string sqlText = "select im.ImageFile from stc.tblAddresses_Images im where im.ImageID = " + ImageId;
string connString = "Provider=SQLOLEDB.1;Password=password;Persist Security Info=True;User ID=username;Initial Catalog=Carddatabase;Data Source=sqlserver";
Code for ScanViewImage.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ScanViewImage.aspx.cs" Inherits="ViewImage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
``</div>``
``</form>``
</body>
</html>
Code for ScanViewImage.aspx.cs:
``using System;
using System.Collections.Generic;`
`using System.Linq;`
`using System.Web;`
`using System.Web.UI;`
`using System.Data;`
`using System.Data.SqlClient;`
`using System.Drawing;`
`using System.Drawing.Imaging;`
`using System.IO;`
`using System.Runtime.InteropServices;`
public partial class ViewImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//get the image id from the url
string ImageId = Request.QueryString["img"];
//ImageId = "477";
//build our query statement
string sqlText = "select im.ImageFile from stc.tblAddresses_Images im where im.ImageID = " + ImageId;
string connString = "Provider=SQLOLEDB.1;Password=password;Persist Security Info=True;User ID=username;Initial Catalog=Carddatabase;Data Source=sqlserver";
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connString);
try{
conn.Open();
System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(sqlText, conn);
System.Data.OleDb.OleDbDataReader dr = cmd.ExecuteReader();
//open the database and get a datareader
if (dr.Read()) //yup we found our image
{
//allow for on the fly conversion from TIFF to JPEG
//Response.ContentType = "image/tiff";
`const byte OLEbyte0 = 21;`
`const byte OLEbyte1 = 28;`
`byte[] abytPic = (byte[])dr[0];`
`// Test for an OLE container header`
`if ((abytPic[0] == OLEbyte0) && (abytPic[1] == OLEbyte1))`
`{`
`string Buffer = string.Empty;`
`int i = 0;`
`Buffer = ByteArrayToString(abytPic, EncodingType.ASCII);`
`//Don't loop through - super duper slooooooooooowwwww!`
`//for (i = 0; i < abytPic.Length; i++)`
`//{`
`// Buffer = Buffer + Convert.ToChar(abytPic[i]);`
`//}`
`//if (Buffer.IndexOf("PBrush") > 0 )`
`//{`
`int BitmapHeaderOffset = Buffer.IndexOf("II");`
`if (BitmapHeaderOffset > 0)`
`{`
`//Calculate the beginning of the bitmap`
`int BitmapOffset = BitmapHeaderOffset;`
`byte[] abytStripped =`
`new byte[abytPic.Length - BitmapOffset];`
`//Move the bitmap into its own array`
`System.Buffer.BlockCopy(abytPic, BitmapOffset, abytStripped,`
`0, abytPic.Length - BitmapOffset);`
`//ReDim ArrBmp(UBound(Arr) - BitmapOffset)`
`//CopyMemory ArrBmp(0), Arr(BitmapOffset), UBound(Arr) -`
`// BitmapOffset + 1`
`//Return the bitmap`
`//`
`//to allow for on the fly TIFF to JPEG conversion`
`//`
`//Response.BinaryWrite(abytStripped);`
`//`
`//convert TIFF byte array to a memory stream pass to a bitmap and write as a jpeg`
`MemoryStream ms = new MemoryStream(abytStripped);`
`Bitmap objBitmap = new Bitmap(ms);`
`Response.ContentType = "image/jpeg";`
`objBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);`
`Response.End();`
`}`
`}`
`}`
`}`
`catch (Exception ex)`
`{ }`
`finally`
`{`
`conn.Close();`
`}`
`}`
`public static string ByteArrayToString(byte[] bytes, EncodingType encodingType)`
`{`
`System.Text.Encoding encoding = null;`
`switch (encodingType)`
`{`
`case EncodingType.ASCII:`
`encoding = new System.Text.ASCIIEncoding();`
`break;`
`case EncodingType.Unicode:`
`encoding = new System.Text.UnicodeEncoding();`
`break;`
`case EncodingType.UTF7:`
`encoding = new System.Text.UTF7Encoding();`
`break;`
`case EncodingType.UTF8:`
`encoding = new System.Text.UTF8Encoding();`
`break;`
`}`
`return encoding.GetString(bytes);`
`}`
`public enum EncodingType`
`{`
`ASCII,`
`Unicode,`
`UTF7,`
`UTF8`
`} `
`}``