Showing posts with label aspx. Show all posts
Showing posts with label aspx. Show all posts

Thursday, March 29, 2012

Direct Display of SQL Image field

Hi

I have a SQL 2000 table in which pictures are stored as an Image column. I want to display then onto a c# aspx webpage without storing them to disk.
What is the best way to read and display the pictures?
In classic ASP I used to do this:

Response.ContentType = "image/jpeg"
Response.BinaryWrite rs.fields("ThisImage")

but I can't get this to work in c#.Kinda, you should use a <img src="http://pics.10026.com/?src=viewImage.aspx?id=RecordID"> and then use some code like this below to read the database:


using System;
using System.Collections;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace DBImages
{
/// <summary>
/// Summary description for ViewImage.
/// </summary>
public class ViewImage : System.Web.UI.Page
{
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}

public ViewImage()
{
Page.Init += new System.EventHandler(Page_Init);
}

private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
//get the image id from the url
string ImageId =Request.QueryString["img"];
double adjustedSize=Convert.ToDouble(Request.QueryString["size"]);

//build our query statement
string sqlText = "SELECT img_data, img_contenttype FROM Image WHERE img_pk = " + ImageId;

SqlConnection connection = new SqlConnection(ConfigurationSettings.Appsettings["DSN"]);
SqlCommand command = new SqlCommand( sqlText, connection);

//open the database and get a datareader
connection.Open();
SqlDataReader dr;
dr = command.ExecuteReader();

if ( dr.HasRows) //yup we found our image
{
dr.Read();

Response.ContentType = dr["img_contenttype"].ToString();

System.IO.Stream fs=new System.IO.MemoryStream((byte[])dr.GetSqlBinary(0) );
System.Drawing.Image _image=System.Drawing.Image.FromStream( new System.IO.MemoryStream((byte[])dr.GetSqlBinary(0)));

int fileLength, fileWidth, fileHeight;

using(System.Drawing.Image image = System.Drawing.Image.FromStream(fs))
{
fileLength = Convert.ToInt32(fs.Length);
fileWidth = image.Width;
fileHeight = image.Height;
}
double multiplier;

if (fileHeight>fileWidth) // picture must be portrait
{
multiplier=Convert.ToDouble(adjustedSize / fileHeight);
}
else
{
multiplier=Convert.ToDouble(adjustedSize / fileWidth);
}
int newWidth, newHeight;

newWidth=(int)(fileWidth * multiplier);
newHeight=(int)(fileHeight * multiplier);

System.Drawing.Image _newimage=_image.GetThumbnailImage(newWidth,newHeight,null,new System.IntPtr());
_newimage.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.BinaryWrite( (byte[]) dr["img_data"] );

}
connection.Close();
}
}
}
}

I do not comment code. If it was hard to write, it should be hard to read. :) I answer some questions tho.|||Thanks for that

Response.BinaryWrite( (byte[]) dr["img_data"] );
does what I want, and I really appreciate the master class on image manipulation.

Direct deployment issues

Hi

I have found article by Jamie T. (www.sqlis.com/26.aspx) where he explains how to use direct configuration.

I followed every step.

Before building project I made sure that CreateDeploymentUtility = True.

Once project was built I have copied files from Deployment folder to other PC to c:\PackageConfiguration (I have 3 files there: environment.dtsconfig, PersonAge.dtsx and jamie.SSISDeploymentManifest)

dtsConfig file is;

<?xml version="1.0" ?>

- <DTSConfiguration>

- <DTSConfigurationHeading>

<DTSConfigurationFileInfo GeneratedBy="me" GeneratedFromPackageName="PersonAge" GeneratedFromPackageID="{2612ABDA-E4FB-43DF-A1C1-44066426A798}" GeneratedDate="7/24/2007 9:46:41 AM" />

</DTSConfigurationHeading>

- <Configuration ConfiguredType="Property" Path="\Package.Connections[Destination].Properties[InitialCatalog]" ValueType="String">

<ConfiguredValue>DataStore</ConfiguredValue>

</Configuration>

- <Configuration ConfiguredType="Property" Path="\Package.Connections[Destination].Properties[ServerName]" ValueType="String">

<ConfiguredValue>MyPC\sql2k5local</ConfiguredValue>

</Configuration>

</DTSConfiguration>

However, I am doing something wrong as I was not able to deploy this to another PC.

I double click Manifest file and in Configuration Package - Property there are two entries: InitailCatalog and ServerName. Initial Catalog has Value=DataStore and ServerName = MyPC\sql2k5local. I change ServerName to be AlexPc\SQL2k5Local.

I have Validate Package after instalation so I get error saying that connection to Destination failed.

If I go to File System to installed package and say Run, package will run OK if I change all ConnectionManagers to point to AlexPc\SQL2k5Local

What is it that I am doing wrong?

I would recomend you to make the deplyment without the deployment utility. If you are using file based packages; you just have to copy the .dtsx and configuration files to the target location. At this point I am not clear wheter you are having problems with the deploymnet or with the package configurations.|||

Thanks Rafael!

it really is simple as that, no need to use utility

ok, next step for me is to try to use indirect configuration

|||

Glad you got it to work!

Here is thread you may find interesting about indirect configurations:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1125596&SiteID=1

|||

This is funny!
I was just looking at this. Issue is that link you had as solution does not work. I was just going to come here and ask you how can I get to that article.

So, please let me know if there is a link that works.

Also, I have found one yesterday on your blog (SSIS package configs using sql server table ...an alternative to Indirect method) but my comment on it is that screen shots were way too small. I was not able to see anything on those.

|||

Also, i have an issue with MSDB; your input would be greatly appreciated.

I am trying to deploy a package to SQL. However, when I open Integration services I can not expand MSDB folder. Error is; SQL Server specified in SSIS service configuration is not present or available.... Login time expired .....(MsDTSSrvr)

I have found post here that says MsDtsSrvr.ini.xml file needs to be changed from

<Name>MSDB</Name>
<ServerName>.</ServerName>

to

<Name>MSDB</Name>
<ServerName>MyServerName</ServerName>

I tried MyServerName - does not work. I also tried value from connecting to Database Engine ( MyServerName\SQL2K5LOCAL) - does not help either.

What am I doing wrong?

this is what I am using:

Microsoft Visual Studio 2005
Version 8.0.50727.42 (RTM.050727-4200)
Microsoft .NET Framework
Version 2.0.50727

Installed Edition: IDE Standard

SQL Server Analysis Services
Microsoft SQL Server Analysis Services Designer
Version 9.00.2047.00

SQL Server Integration Services
Microsoft SQL Server Integration Services Designer
Version 9.00.2047.00

|||

I have updated the link:

http://rafael-salas.blogspot.com/2007/01/ssis-package-configurations-using-sql.html

I never have deployed the packages to the DB; but I know this forums has some threads that talks about not being able to expand the MSDB folder. USe the search capability of the forum

sql