473,386 Members | 1,823 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,386 software developers and data experts.

save uploaded image thumbnail to sql db image field

Hi there,
Trying to allow client to upload image, thumbnail it, and save it into
sql table image field.
Code compiles & runs but image cannot be retrieved. any clues what I'm
doing wrong pls !
Dim origImage As System.Drawing.Image =
System.Drawing.Image.FromStream(FileUpload1.Posted File.InputStream)

' rewind the input stream to read it again to store the
original image in the database
FileUpload1.PostedFile.InputStream.Seek(0, SeekOrigin.Begin)

'make thumbnail

Dim thumbnail As System.Drawing.Image = New
System.Drawing.Bitmap(120, 120)
Dim thisFormat = origImage.RawFormat
Dim ms As MemoryStream = New MemoryStream()
thumbnail.Save(ms, thisFormat)
'System.Drawing.Imaging.ImageFormat.Jpeg)
ms.Seek(0, SeekOrigin.Begin)

Dim buffer(ms.Length) As Byte
ms.Read(buffer, 0, buffer.Length)

Dim cnn As New
SqlConnection(System.Configuration.ConfigurationMa nager.ConnectionStrings(1).ConnectionString)
cnn.Open()

Dim cmd As New SqlCommand("UPDATE STAFF SET PIC=@img WHERE
STAFF_ID=26", cnn)

Dim ImageContent(buffer.Length) As Byte
Dim intStatus As Integer
intStatus = ms.Read(ImageContent, 0, buffer.Length)
Dim prmImage As New SqlParameter("@img", SqlDbType.Image)
prmImage.Value = ImageContent
cmd.Parameters.Add(prmImage)

cmd.ExecuteNonQuery()
cmd.Dispose()
cnn.Close()
cnn.Dispose()
Response.Write("done")
Dec 14 '07 #1
1 3115
Your code that puts image into DB looks good except i do not see anywere
where you actually put a thumbnail image into
variable 'thumbnail', you just create empty thumbnail and never make an
actual picture.
but may be you jsut skiped that code.
then probably youe code that reads image from DB is bad

PS: MemoryStream has GetBuffer method..

That would save you creation of ImageContent byte array.

George
"dodgeyb" <ch***@ctlsoft.comwrote in message
news:b8**********************************@s12g2000 prg.googlegroups.com...
Hi there,
Trying to allow client to upload image, thumbnail it, and save it into
sql table image field.
Code compiles & runs but image cannot be retrieved. any clues what I'm
doing wrong pls !
Dim origImage As System.Drawing.Image =
System.Drawing.Image.FromStream(FileUpload1.Posted File.InputStream)

' rewind the input stream to read it again to store the
original image in the database
FileUpload1.PostedFile.InputStream.Seek(0, SeekOrigin.Begin)

'make thumbnail

Dim thumbnail As System.Drawing.Image = New
System.Drawing.Bitmap(120, 120)
Dim thisFormat = origImage.RawFormat
Dim ms As MemoryStream = New MemoryStream()
thumbnail.Save(ms, thisFormat)
'System.Drawing.Imaging.ImageFormat.Jpeg)
ms.Seek(0, SeekOrigin.Begin)

Dim buffer(ms.Length) As Byte
ms.Read(buffer, 0, buffer.Length)

Dim cnn As New
SqlConnection(System.Configuration.ConfigurationMa nager.ConnectionStrings(1).ConnectionString)
cnn.Open()

Dim cmd As New SqlCommand("UPDATE STAFF SET PIC=@img WHERE
STAFF_ID=26", cnn)

Dim ImageContent(buffer.Length) As Byte
Dim intStatus As Integer
intStatus = ms.Read(ImageContent, 0, buffer.Length)
Dim prmImage As New SqlParameter("@img", SqlDbType.Image)
prmImage.Value = ImageContent
cmd.Parameters.Add(prmImage)

cmd.ExecuteNonQuery()
cmd.Dispose()
cnn.Close()
cnn.Dispose()
Response.Write("done")

Dec 14 '07 #2

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

Similar topics

0
by: Mark | last post by:
Hi all, I want to be able to resize an image that a user has uploaded. I have the image upload working as well as the image resize. When I go to delete the original uploaded file (I want to...
1
by: bart plessers | last post by:
Hello, For a while I am working with ThumbsPlus ( http://www.cerious.com/ ) as manager for pics. The benefit of the program is that it stores all kind of information in a central Microsoft...
0
by: Matt Brelje | last post by:
I have an .aspx page from which users can upload an image. After a file is uploaded, I also save a 60x60 pixel thumbnail of that image. Works great. What I can't seem to do is save a CROPPED...
9
by: Ivan Demkovitch | last post by:
Hi! I would like to know if I can save File on Server using server-side code? For example, I like to create thumbnail images and populate specific directory. Do I need specific permissions...
5
by: Brian Lowe | last post by:
My web site accepts uploaded photos and stores them in a SQL table as BLObs so they never touch the filesystem. I have a way to create a thumbnail version of the uploaded image and store that in...
2
by: NATO24 | last post by:
Hello, I am trying to write a sub routine that I can pass a posted (image) file to. That routine will save the original file, then create a thumbnail and save it. When I try to create the image...
3
by: neovantage | last post by:
Hey all, i have created image by mixing logo and the original image. It create image and show in the same window. I have done 3 steps for this. First i upload an image to my server by using...
7
by: sandeepk84 | last post by:
Hi all... I have file upload option in my web application. I use Struts 2. I wanted to show a preview of the uploaded file to the user. The uploaded file can be of any type like...
1
by: chennaibala | last post by:
can any one send me mutiple image upload program and save the file name with extension in mysql table.we must cheak uploaded file type like bmp or any image file while uploading. i develop...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.