Skip Navigation Links.

Northwind\WebControls\ImageHandlerBase.cs

D:\Temp3\TestProjects\CSADO.Net\Northwind\WebControls\ImageHandlerBase.cs
/*
 * This file was generated by ProCG version 2.0
 *
 * File name:	Northwind\WebControls\ImageHandlerBase.cs
 * Language:	C# - ADO.Net 
 * Database:	My Sql
 *
 * Copyright (c) 2002-2019 iGenXSoft.
 * For more information visit http://www.igenxsoft.com
 */


using System;
using System.Web;
using System.Web.SessionState;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;
using Northwind.General;
using Northwind.Data;
using Northwind.WebControls;

public class ImageHandlerBase : IHttpHandler, IRequiresSessionState
{

	protected string primaryKey;
	protected string id;
	protected HttpContext context;

	
	public virtual void ProcessRequest (HttpContext context) {

		this.context = context;
		id = context.Request.QueryString["id"];
							
		GetImageFromSession();

	}

	protected virtual void GetImageFromSession()
	{
		byte[] picture = (byte[])context.Session[id];
		WriteImageToResponse(picture);
	}

	protected virtual void WriteImageToResponse(byte[] picture)
	{
		if (picture != null)
		{
			int offset;
			string mimeType = MimeTypeFrom(picture, null);
			if (mimeType.Equals("application/octet-stream")) // means it has ole wrapper like picture in table categories in Nortwind database
			{
				context.Response.ContentType = "image/bmp";
				offset = 78;
			}
			else
			{
				context.Response.ContentType = mimeType;
				offset = 0;
			}
			context.Response.OutputStream.Write(picture, offset, picture.Length - offset);
		}
	}
	 
	public virtual bool IsReusable {
		get {
			return false;
		}
	}

	public virtual CNorthwindUser User
	{
		get
		{
			return SessionManager.User;
		}
	}

 
	[System.Runtime.InteropServices.DllImport("urlmon.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = false)]
	static extern int FindMimeFromData(IntPtr pBC,
						[MarshalAs(UnmanagedType.LPWStr)] string pwzUrl,
						[MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.I1, SizeParamIndex=3)] 
						byte[] pBuffer,
						int cbSize,
						[MarshalAs(UnmanagedType.LPWStr)]  string pwzMimeProposed,
						int dwMimeFlags,
						out IntPtr ppwzMimeOut,
						int dwReserved);
	  
	public virtual string MimeTypeFrom(byte[] dataBytes, string mimeProposed) {
		if (dataBytes == null)
			throw new ArgumentNullException("dataBytes");
		string mimeRet = String.Empty;
		IntPtr suggestPtr = IntPtr.Zero, filePtr = IntPtr.Zero, outPtr = IntPtr.Zero;
		if (mimeProposed != null && mimeProposed.Length > 0) {
			//suggestPtr = Marshal.StringToCoTaskMemUni(mimeProposed); // for your experiments ;-)
			mimeRet = mimeProposed;
		}
		int ret = FindMimeFromData(IntPtr.Zero, null, dataBytes, dataBytes.Length, mimeProposed, 0, out outPtr, 0);
		if (ret == 0 && outPtr != IntPtr.Zero) {
			//todo: this leaks memory outPtr must be freed
			return Marshal.PtrToStringUni(outPtr);
		}
		
		System.Diagnostics.Trace.Write("MimeType is " + MimeTypeFrom(Encoding.ASCII.GetBytes("%PDF-"), "text/plain"));

		return mimeRet;
	}

	
}

  //      3136 ProCG uses this line - don't edit it