Retrieving Images from a Database, Saving in a different formats

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
June 05, 2002
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Retrieving Images from a Database, Saving as Different Format

"I am using the information from the article listed in the subject line in a project I am currently involved in. We are producing a web application that contains an image library of artifacts for a local museum. I have taken your idea and implemented it in my project and it is working great!! BTW, great article!!! I only have one issue to deal with, and I was hoping one of you could point me in the right direction in figuring this out.  

The users of the application have the need to save the picture back to a file for other purposes (sell to other museum, put up on a web site, etc…). I figured they could just Right-Click the image and select Save As and save the picture. Sounded simple enough. When I attempt to save the picture, the only file type I have available is Bitmap (*.bmp). I can save the picture as a Bitmap and then open it in an editor (MS Paint) and Save As whatever type of file I want. I am afraid that will be one (or two) too many steps for some of these users (they are kind of challenged if you know what I mean.and I know you know what I mean).  

I know I could solve the whole problem (and probably speed up the application) by saving the images to a server on the network, but these are sensitive images with digital watermarks and so forth. This is why I like encapsulating the images in the database. That way only the users with access to the application can access the images.   Any ideas or direction you can offer is really appreciated. Thanks. "

Here is the answer Matt

"I figured it out. Here is the code if you are interested. It is quite rough right now and without proper exception try-catch blocks, but it does work well. Also, the SQL statement is different because I am using this page to pull only the ThumbNail image from a table which stores Thumbnails along with High-Res images. Thanks for the great work on the .NET sites. They really save me a ton of time."

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here
'Extract the ID out of the URL

Dim imgID As String

imgID = Request.QueryString("id")  

'Uses the System.Configuration Class to extract DSN string from the web.config

Dim objConnection As SqlConnection

objConnection = New SqlConnection(ConfigurationSettings.AppSettings("imgarchive"))

Dim objCommand As SqlCommand = New SqlCommand()

  objCommand.Connection = objConnection

objCommand.CommandText = "select DigitalFile, ContentType from tblDigitization " & _

"where (ThumbNail=1) and (ImageID=" & imgid & ")"

 

objConnection.Open()

Dim dr As SqlDataReader = objCommand.ExecuteReader()

If dr.Read() Then

Dim imgContentType As String = (dr.Item(("ContentType")).ToString())

Dim imgByte(dr.GetBytes(0, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte

dr.GetBytes(0, 0, imgByte, 0, imgByte.Length)

Dim imgStream As MemoryStream = New MemoryStream(imgByte)

Dim img As Image = Image.FromStream(imgStream)

Response.ContentType = imgContentType

'set the proper image format

If (InStr(imgContentType, "jpeg")) Then

img.Save(Response.OutputStream, ImageFormat.Jpeg)

ElseIf (InStr(imgContentType, "gif")) Then

img.Save(Response.OutputStream, ImageFormat.Gif)

End If

Response.Flush()

Response.Close()

imgStream.Close()

End If  

objConnection.Close()

End Sub

blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials