473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Better solution ?

LB
Hello everybody,
I'm trying to display into a system.Web.UI.WebControls.Image a picture
coming from the DB.
I used the code below, and after specifying my image1.url property, the
image is displayed.

But now, I have still the image stored in my "DestFilePath " directory.
Is there a way to directly read the blob, and assign it to my
system.Web.UI.WebControls.Image without writing the file itself to the disk
?

Thank you so much in advance
LB
Public Sub GetBlobInfo(DestFilePath As String)
Try
Dim PictureCol As Integer = 0 ' the column # of the BLOB field
Dim cn As New SqlConnection("server=localhost;integrated
security=yes;database=SomeDB")
Dim cmd As New SqlCommand("SELECT Picture FROM mytable WHERE
somecondition='Test'", cn)
cn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue)) As
[Byte]
dr.GetBytes(PictureCol, 0, b, 0, b.Length)
dr.Close()
cn.Close()
Dim fs As New System.IO.FileStream(DestFilePath,
System.IO.FileMode.Create, System.IO.FileAccess.Write)

fs.Write(b, 0, b.Length)
fs.Close()
MessageBox.Show("Image written to file successfully")
Catch ex As SqlException
MessageBox.Show(ex.Message)
End Try
End Sub
Nov 19 '05 #1
4 1099
The standard solution is to a have a special web page, called, for example,
GetImage.aspx. Your image url will point to the page:

ImageUrl="GetImage.aspx?id=myImageId"

The page will find the image in the database according to id and stream it
down to the client in the response stream.

Eliyahu

"LB" <1@1.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello everybody,
I'm trying to display into a system.Web.UI.WebControls.Image a picture
coming from the DB.
I used the code below, and after specifying my image1.url property, the
image is displayed.

But now, I have still the image stored in my "DestFilePath " directory.
Is there a way to directly read the blob, and assign it to my
system.Web.UI.WebControls.Image without writing the file itself to the disk ?

Thank you so much in advance
LB
Public Sub GetBlobInfo(DestFilePath As String)
Try
Dim PictureCol As Integer = 0 ' the column # of the BLOB field
Dim cn As New SqlConnection("server=localhost;integrated
security=yes;database=SomeDB")
Dim cmd As New SqlCommand("SELECT Picture FROM mytable WHERE
somecondition='Test'", cn)
cn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue)) As
[Byte]
dr.GetBytes(PictureCol, 0, b, 0, b.Length)
dr.Close()
cn.Close()
Dim fs As New System.IO.FileStream(DestFilePath,
System.IO.FileMode.Create, System.IO.FileAccess.Write)

fs.Write(b, 0, b.Length)
fs.Close()
MessageBox.Show("Image written to file successfully")
Catch ex As SqlException
MessageBox.Show(ex.Message)
End Try
End Sub

Nov 19 '05 #2
Sure. You fetch it from the db, convert the array of bytes to a stream, sets
the Response.ContentType to "image/jpg", and save the stream to the
Response.OutputStream. This can be done by creating an ASPX page that serves
as the "image url."

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

"LB" <1@1.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello everybody,
I'm trying to display into a system.Web.UI.WebControls.Image a picture
coming from the DB.
I used the code below, and after specifying my image1.url property, the
image is displayed.

But now, I have still the image stored in my "DestFilePath " directory.
Is there a way to directly read the blob, and assign it to my
system.Web.UI.WebControls.Image without writing the file itself to the
disk ?

Thank you so much in advance
LB
Public Sub GetBlobInfo(DestFilePath As String)
Try
Dim PictureCol As Integer = 0 ' the column # of the BLOB field
Dim cn As New SqlConnection("server=localhost;integrated
security=yes;database=SomeDB")
Dim cmd As New SqlCommand("SELECT Picture FROM mytable WHERE
somecondition='Test'", cn)
cn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue)) As
[Byte]
dr.GetBytes(PictureCol, 0, b, 0, b.Length)
dr.Close()
cn.Close()
Dim fs As New System.IO.FileStream(DestFilePath,
System.IO.FileMode.Create, System.IO.FileAccess.Write)

fs.Write(b, 0, b.Length)
fs.Close()
MessageBox.Show("Image written to file successfully")
Catch ex As SqlException
MessageBox.Show(ex.Message)
End Try
End Sub

Nov 19 '05 #3
So just to add to what the other two said, here is an example, this is the
load event of a page called GetImage.aspx:

Response.Cache.SetCacheability(HttpCacheability.No Cache)
Response.Clear()
Response.ContentType = "image/jpg"
Dim map As Bitmap = CType(Session("map"), Bitmap) ''you would get the image
from the db here
Dim ms As New MemoryStream
map.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Response.BinaryWrite(ms.ToArray)
Response.End()
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:ep**************@TK2MSFTNGP14.phx.gbl...
Sure. You fetch it from the db, convert the array of bytes to a stream,
sets the Response.ContentType to "image/jpg", and save the stream to the
Response.OutputStream. This can be done by creating an ASPX page that
serves as the "image url."

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
What You Seek Is What You Get.

"LB" <1@1.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello everybody,
I'm trying to display into a system.Web.UI.WebControls.Image a picture
coming from the DB.
I used the code below, and after specifying my image1.url property, the
image is displayed.

But now, I have still the image stored in my "DestFilePath " directory.
Is there a way to directly read the blob, and assign it to my
system.Web.UI.WebControls.Image without writing the file itself to the
disk ?

Thank you so much in advance
LB
Public Sub GetBlobInfo(DestFilePath As String)
Try
Dim PictureCol As Integer = 0 ' the column # of the BLOB field
Dim cn As New SqlConnection("server=localhost;integrated
security=yes;database=SomeDB")
Dim cmd As New SqlCommand("SELECT Picture FROM mytable WHERE
somecondition='Test'", cn)
cn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue)) As
[Byte]
dr.GetBytes(PictureCol, 0, b, 0, b.Length)
dr.Close()
cn.Close()
Dim fs As New System.IO.FileStream(DestFilePath,
System.IO.FileMode.Create, System.IO.FileAccess.Write)

fs.Write(b, 0, b.Length)
fs.Close()
MessageBox.Show("Image written to file successfully")
Catch ex As SqlException
MessageBox.Show(ex.Message)
End Try
End Sub


Nov 19 '05 #4
Here's a sample that does something very similar:

http://staff.develop.com/ballen/blog...8-9aff48bf2481

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hello everybody,
I'm trying to display into a system.Web.UI.WebControls.Image a picture
coming from the DB.
I used the code below, and after specifying my image1.url property,
the
image is displayed.
But now, I have still the image stored in my "DestFilePath "
directory.
Is there a way to directly read the blob, and assign it to my
system.Web.UI.WebControls.Image without writing the file itself to the
disk
?
Thank you so much in advance
LB
Public Sub GetBlobInfo(DestFilePath As String)
Try
Dim PictureCol As Integer = 0 ' the column # of the BLOB field
Dim cn As New SqlConnection("server=localhost;integrated
security=yes;database=SomeDB")
Dim cmd As New SqlCommand("SELECT Picture FROM mytable WHERE
somecondition='Test'", cn)
cn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue))
As
[Byte]
dr.GetBytes(PictureCol, 0, b, 0, b.Length)
dr.Close()
cn.Close()
Dim fs As New System.IO.FileStream(DestFilePath,
System.IO.FileMode.Create, System.IO.FileAccess.Write)
fs.Write(b, 0, b.Length)
fs.Close()
MessageBox.Show("Image written to file successfully")
Catch ex As SqlException
MessageBox.Show(ex.Message)
End Try
End Sub


Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: david | last post by:
I write a program which returns the maximum of three input integers. both can run weil in the DEV-C++ , but I just wonder which one is better, and I also want to make it clear that if I use the...
16
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
43
by: Rob R. Ainscough | last post by:
I realize I'm learning web development and there is a STEEP learning curve, but so far I've had to learn: HTML XML JavaScript ASP.NET using VB.NET ..NET Framework ADO.NET SSL
11
by: Daylor | last post by:
hi. im using option strict on. im doing in ,from the simple reason ,to be warn when there are implict conversion like string to int ,int to string. BUT. the price ,(now i see ), is very bad....
9
by: seberino | last post by:
I have been using distuils for a while and was wondering when Python Eggs (new project) is better? So basically Python Eggs precompiles and compresses binaries for you so you just have to load...
8
by: hugh webster | last post by:
MySql seems to only accept dates as 'yyyy-mm-dd'. How do I do that when the user might input dd/mm/yy, or d.m.yyyy (I'm in Europe)? I know I can do sscanf, or explode to rebuild the date string -...
39
by: windandwaves | last post by:
Hi Folk I have to store up to eight boolean bits of information about an item in my database. e.g. with restaurant drive-through facility yellow windows
2
by: kaferro | last post by:
I use the following code to reset an ID when an order has been filled by more than one trade. For example, order= buy 9 corn, order ID = 101. If the order is filled with three separate trades of,...
13
by: S James S Stapleton | last post by:
I have some code, and I want to make it future-resistant. I have a bunch of variables that are set up run-time, and once set up, should act as constants. I don't want to #define them, because their...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.