473,406 Members | 2,847 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,406 software developers and data experts.

jpeg from database to file

Hi. I have an asp.net 1.1 application that I need to retrieve a jpeg
image from a database and write that to the local (not client's) file
system.

What I'm doing is a club login page, and each club can have a logo
stored in the sql database. In the past I have streamed an image
directly to the browser, but this time I want to write it as a jpg file
to the local (to the application) file system. The reason being that
this will be reloaded a number of times and I want to cache it so it
doesn't need to be retrieved from the database every time someone visits
the page.

Can anyone give me any hints? Currently, I'm converting the image data
to a bit array. How do I save that array to a file? Thanks!

Matt
Jul 10 '07 #1
4 1593
Matt,

You can use System.IO namespace:

Dim outStream As Stream = New FileStream(fileName, FileMode.CreateNew)
With New MemoryStream(bytes)
.WriteTo(outStream)
End With

Alternatively, you could use .Net Cache to store images - it is specifically
designed for cases like yours.
"MattB" wrote:
Hi. I have an asp.net 1.1 application that I need to retrieve a jpeg
image from a database and write that to the local (not client's) file
system.

What I'm doing is a club login page, and each club can have a logo
stored in the sql database. In the past I have streamed an image
directly to the browser, but this time I want to write it as a jpg file
to the local (to the application) file system. The reason being that
this will be reloaded a number of times and I want to cache it so it
doesn't need to be retrieved from the database every time someone visits
the page.

Can anyone give me any hints? Currently, I'm converting the image data
to a bit array. How do I save that array to a file? Thanks!

Matt
Jul 11 '07 #2
Hi Matt,

MattB schrieb:
Hi. I have an asp.net 1.1 application that I need to retrieve a jpeg
image from a database and write that to the local (not client's) file
system.
When you have the byte array, you can simply open a filestream and pass
the byte array to the write method.

As an alternative, create a memorystream, write your byte array into
that, then create an image from the memorystream and call the save
method on the image object. This is handy if you need to save in a
different format or need to do some other operations on the image anyway.

Keep in mind that the account your webapp is running under needs write
permissions to the directory you want to save the image into.

Hope this helps,

Roland
Jul 11 '07 #3
Sergey Poberezovskiy wrote:
Matt,

You can use System.IO namespace:

Dim outStream As Stream = New FileStream(fileName, FileMode.CreateNew)
With New MemoryStream(bytes)
.WriteTo(outStream)
End With

Alternatively, you could use .Net Cache to store images - it is specifically
designed for cases like yours.
"MattB" wrote:
>Hi. I have an asp.net 1.1 application that I need to retrieve a jpeg
image from a database and write that to the local (not client's) file
system.

What I'm doing is a club login page, and each club can have a logo
stored in the sql database. In the past I have streamed an image
directly to the browser, but this time I want to write it as a jpg file
to the local (to the application) file system. The reason being that
this will be reloaded a number of times and I want to cache it so it
doesn't need to be retrieved from the database every time someone visits
the page.

Can anyone give me any hints? Currently, I'm converting the image data
to a bit array. How do I save that array to a file? Thanks!

Matt
Great. Thanks for the tip (and thanks to Roland too). I tried this and
I'm getting a file created, but it's size is 0 bytes. Here's the whole
block of code. Can you see where I'm going wrong?

Dim strHex As String = dtAct.Rows(0)("webgrpimg")
'strip out characters that mess up the conversion
If ConfigurationSettings.AppSettings("UseWebService") = "1" Then
'for what ever reason, when going through the web service, the line
endings need to be stripped like this
strHex = strHex.Replace(vbCr, Nothing)
strHex = strHex.Replace(vbLf, Nothing)
Else
strHex = strHex.Replace(vbCrLf, Nothing)
End If
strHex = strHex.Replace(vbTab, Nothing)

'Once the bad characters are stripped, convert hex string to bit array
Dim MyData(Len(strHex) / 2) As Byte
Dim i As Int32, er As String
For i = 0 To (Len(strHex) / 2) - 1
Try
MyData(i) = CByte(CLng("&H" & Mid(strHex, (2 * i) + 1, 2)))
Catch ex As Exception
er = ex.Message
End Try
Next

If Not Directory.Exists(Server.MapPath("images/clubs")) Then
Directory.CreateDirectory(Server.MapPath("images/clubs"))
End If
If Not Directory.Exists(Server.MapPath("images/clubs/" &
dtAct.Rows(0)("acct_name"))) Then
Directory.CreateDirectory(Server.MapPath("images/clubs/" &
dtAct.Rows(0)("acct_name")))
End If
Dim outStream As Stream = New FileStream(strLogoPath, FileMode.CreateNew)
With New MemoryStream(MyData)
.WriteTo(outStream)
End With

------------------

Thanks!

Matt
Jul 11 '07 #4
Never mind! I got it sorted out by adding a outStream.Close at the end.
Thanks again!

Matt

MattB wrote:
Sergey Poberezovskiy wrote:
>Matt,

You can use System.IO namespace:

Dim outStream As Stream = New FileStream(fileName, FileMode.CreateNew)
With New MemoryStream(bytes)
.WriteTo(outStream)
End With

Alternatively, you could use .Net Cache to store images - it is
specifically designed for cases like yours.
"MattB" wrote:
>>Hi. I have an asp.net 1.1 application that I need to retrieve a jpeg
image from a database and write that to the local (not client's) file
system.

What I'm doing is a club login page, and each club can have a logo
stored in the sql database. In the past I have streamed an image
directly to the browser, but this time I want to write it as a jpg
file to the local (to the application) file system. The reason being
that this will be reloaded a number of times and I want to cache it
so it doesn't need to be retrieved from the database every time
someone visits the page.

Can anyone give me any hints? Currently, I'm converting the image
data to a bit array. How do I save that array to a file? Thanks!

Matt

Great. Thanks for the tip (and thanks to Roland too). I tried this and
I'm getting a file created, but it's size is 0 bytes. Here's the whole
block of code. Can you see where I'm going wrong?

Dim strHex As String = dtAct.Rows(0)("webgrpimg")
'strip out characters that mess up the conversion
If ConfigurationSettings.AppSettings("UseWebService") = "1" Then
'for what ever reason, when going through the web service, the line
endings need to be stripped like this
strHex = strHex.Replace(vbCr, Nothing)
strHex = strHex.Replace(vbLf, Nothing)
Else
strHex = strHex.Replace(vbCrLf, Nothing)
End If
strHex = strHex.Replace(vbTab, Nothing)

'Once the bad characters are stripped, convert hex string to bit array
Dim MyData(Len(strHex) / 2) As Byte
Dim i As Int32, er As String
For i = 0 To (Len(strHex) / 2) - 1
Try
MyData(i) = CByte(CLng("&H" & Mid(strHex, (2 * i) + 1, 2)))
Catch ex As Exception
er = ex.Message
End Try
Next

If Not Directory.Exists(Server.MapPath("images/clubs")) Then
Directory.CreateDirectory(Server.MapPath("images/clubs"))
End If
If Not Directory.Exists(Server.MapPath("images/clubs/" &
dtAct.Rows(0)("acct_name"))) Then
Directory.CreateDirectory(Server.MapPath("images/clubs/" &
dtAct.Rows(0)("acct_name")))
End If
Dim outStream As Stream = New FileStream(strLogoPath, FileMode.CreateNew)
With New MemoryStream(MyData)
.WriteTo(outStream)
End With

------------------

Thanks!

Matt
Jul 11 '07 #5

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

Similar topics

0
by: UnixUser | last post by:
I am using the PHP 4.3.4 with a standard (unmodified ) php.ini file. I am tryong to output a blob field that contains a jpeg image using the ibase_blob_echo( row) function. I am presently...
2
by: Suzanne | last post by:
Hi all, I'm attempting to save images (that are in jpeg format) to a database column of type image. I've read over the Microsoft Knowledge Base articles for this and I can get things working...
16
by: David Lauberts | last post by:
Hi Wonder if someone has some words of wisdom. I have a access 2002 form that contains 2 graph objects that overlay each other and would like to export them as a JPEG to use in a presentation....
4
by: Rednelle | last post by:
Greetings all, As a newbie, using Access 2000, I would appreciate advice on the best way to include pictures. I have developed a 'Home Inventory' database which can include jpeg thumbnails of...
3
by: RobertH | last post by:
Hello all. I have been hacking away trying to get a SQL image (jpeg) to render in a control or table row Without using the Response.BinaryWrite.... I think i might be on the verge but need a...
9
by: Tim | last post by:
Since rtf files cause so many troubles, i.e., allow the users to download the files, slow loading, incompatible with other browser, and so on. Do you think I should convert them to JPEG files to...
10
by: rtilley | last post by:
Hope it's not inappropriate to post this here. Could someone critique my code? I have no Python programmers in my office to show this to. The script works OK, but should I do it differently? I...
0
by: Jack Wu | last post by:
Hi I've spent a good majority of my day trying to figure out how to have PIL 1.1.5 working on my OSX 10.3.9_PPC machine. I'm still stuck and I have not gotten anywhere. Could somebody please...
1
by: Masood | last post by:
Greetings, We are writing an application wherein we have to display jpeg images, we are thinking of using the IJG JPEG LIB written in C code. Will be greatfull if someone could guide us as to...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.