Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

ImageHandler question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gsauns - 19 Dec 2007 22:48 GMT
I have a Generic Handler file that I use to display images stored in a
database (SQL 2005, stored as varbinary fields).

I pulled most of the code from the web, and it works great. I have
recently added functionality to change the photo via selections from a
radiobuttonlist. That also works great. What I would like to do is to
display an alternate image instead of the alternate text. I have the
image in a .jpg file. The question is, how can I get the MemoryStream
from this .jpg file? Is it possible to read that from a saved jpg on
the file system? I will always know where that alternate image resides
on the server. Here is the handler code:

<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.IO;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public class ImageHandler : IHttpHandler {
  public bool IsReusable {
     get {
        return true;
     }
  }
  public static Stream GetPhoto2(int photoid, string strSelection)
  {
      using (SqlConnection connection = new
SqlConnection(ConfigurationManager.ConnectionStrings["Conn_String"].ConnectionString))
     {
        using (SqlCommand command = new SqlCommand(strSelection,
connection))
        {
           command.Parameters.Add(new SqlParameter("@id", photoid));
           connection.Open();
           object result = command.ExecuteScalar();
           try
           {
              return new MemoryStream((byte[])result);
           }
           catch
           {
              // MemoryStream of alternate image would be read in
here.
              return null;
           }
        }
     }
  }
  public void ProcessRequest (HttpContext context)
  {
    //Set up the response settings
     context.Response.ContentType = "image/jpeg";
     context.Response.Cache.SetCacheability(HttpCacheability.Public);
     context.Response.BufferOutput = false;
     // Setup the PhotoID Parameter
     string sel;
     Int32 id = -1;
     Stream stream = null;
     id = Convert.ToInt32(context.Request.QueryString["cplyr_id"]);
     switch (context.Request.QueryString["img"].ToString())
     {
         case "front":
             sel = "SELECT FRONT_PHOTO FROM TABLE WHERE ID=@id";
             break;
         case "back":
             sel = "SELECT BACK_PHOTO FROM TABLE WHERE ID=@id";
             break;
         case "head":
             sel = "SELECT HEAD_PHOTO FROM TABLE WHERE ID=@id";
             break;
         default:
             sel = "SELECT FRONT_PHOTO FROM TABLE WHERE ID=@id";
             break;
     }
     stream = GetPhoto2(id,sel);
     const int buffersize = 1024 * 16;
     byte[] buffer2 = new byte[buffersize];
     int count = stream.Read(buffer2, 0, buffersize);
     while (count > 0) {
        context.Response.OutputStream.Write(buffer2, 0, count);
        count = stream.Read(buffer2, 0, buffersize);
     }
  }
}
marss - 20 Dec 2007 05:58 GMT
> I have the
> image in a .jpg file. The question is, how can I get the MemoryStream
> from this .jpg file? Is it possible to read that from a saved jpg on
> the file system?

Try this
string path = Server.MapPath("~/images/altimage.jpg");
MemoryStream ms = new MemoryStream(File.ReadAllBytes(path));

Regards,
Mykola
http://marss.co.ua
gsauns - 20 Dec 2007 19:01 GMT
Thanks! That did the trick for me. Just needed to alter your one line
a bit, being that this was a Handler file:

string path = HttpContext.Current.Server.MapPath("~/images/
altimage.jpg");
MemoryStream ms = new MemoryStream(File.ReadAllBytes(path));

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.