By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,762 Members | 1,838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,762 IT Pros & Developers. It's quick & easy.

Error saving resized bitmap

P: n/a
I get a generic error (not very helpful) when attempting to save a
re-sized image back to its original location. Here is the code snippet:

Dim g As System.Drawing.Image =
System.Drawing.Image.FromFile(Me.File1.Value)
Dim thisFormat = g.RawFormat
Dim imgOutput As New Bitmap(g, 800, 600)
Dim strFileType As String

Dim strFileName As String = File1.PostedFile.FileName

' only the attched file name not its path
Dim c As String = System.IO.Path.GetFileName(strFileName)

imgOutput.Save(strFileName)

The last line seems to throw the error. With a hard coded path, I don't
get an error so I think somehow I am missing something. The only
difference is the missing ("") around the strFileName vs. a hard coded
path.

Any ideas?
Thank you and I appreciate your help.

Mar 15 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
It looks like you are trying to upload a file from your client and then
save it to an equivalent directory on the server. I don't think that's
what you intended. You need to save it to a mapped path on the server
(one with write permissions) and then download it to the original
location on the client... I think :-)

Gary

Mar 15 '06 #2

P: n/a
Ok. What you say makes sense. But how do I download it to the original
location?

What I'd like to do is take the new bitmap and save it to a SQL Server
database. (I can already hear a dozen people telling me its easier NOT
to do that, but it's the project requirement - my hands are tied).

I can save to a databse from the htmlinputfile control, but am
struggling with adapting it to the bitmap I've created.
What should be happening is that I take an image, if it is greater than
a certain size, resize it and then save it to the database. I can
resize the image and save it to the server. I can take an image and
upload it to a database. But I can't seem to take the image, resize it
and then save the new version to the database.

I appreciate the help!

Currently, I'm trying to adapt my functions to pull the data I need
from the bitmap rather than the input control.
However, I thought 'wouldn't it be cool, if I could take the image,
resize it, save the new version to the same location already in the
file input control and then upload it to the database'

Sounds good but not working.

Mar 15 '06 #3

P: n/a
I think you're complicating things - just write the resized file out to
a mapped server path, store it in your SQL server and then delete the
file. Don't worry about overwriting the posted file, AFAIK it does not
live anywhere on the server other than in memory (happy to be
corrected) until you physically save it. Obviously the original image
on the client will be unchanged, but I assume you don't care about
that.

All the best,

Gary

Mar 15 '06 #4

P: n/a

<ga***@3inabed.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
I think you're complicating things - just write the resized file out to
a mapped server path, store it in your SQL server and then delete the
file. Don't worry about overwriting the posted file, AFAIK it does not
live anywhere on the server other than in memory (happy to be
corrected) until you physically save it. Obviously the original image
on the client will be unchanged, but I assume you don't care about
that.

All the best,

Gary


If you are going to store the image into a field/record on SQL Server, you
don't need to write the file to disk on the server first. When you get it
into an Image object, save the image to a MemoryStream. From there, read
from the memory stream into a byte-array and store that data in the "image"
field in SQL Server.

Mythran

Mar 15 '06 #5

P: n/a
Thanks! The MemoryStream was what I was lacking.
And I also didn't think about the convenience of storing the file on
the server and then deleting, that would have been a little more
helpful as well.

But to add - I did find that if I simply disposed of the original
graphic (g.dispose()), I was able to save to the original location.
However that didn't help either. Because I did not know how to get the
image to a stream of some type to a byte array (as I was doing with the
input file), I was simply trying to think out of the box for another
way around the obstacle.

I appreciate the help all around.

Mar 15 '06 #6

P: n/a
One more quick question -
I've done some researching and can't seem to find a straight answer to
this. In the following code, how do I actually save to the stream?
Here is what I have:
Dim intFileLength As Integer, bytData() As Byte
Dim objStream As System.IO.MemoryStream
'If FileFieldSelected(FileField) Then
intFileLength = FileField.PropertyItems.Length
ReDim bytData(intFileLength)
objStream = ?????? 'Don't know what goes here
objStream.Read(bytData, 0, intFileLength)
Return bytData

With the input file, I have this: objStream =
FileField.PostedFile.InputStream

My suspicion is to save it like this: FileField.Save(objStream,
Imaging.ImageFormat.Jpeg)

Mar 15 '06 #7

P: n/a
One more quick question -
I've done some researching and can't seem to find a straight answer to
this. In the following code, how do I actually save to the stream?
Here is what I have:
Dim intFileLength As Integer, bytData() As Byte
Dim objStream As System.IO.MemoryStream
'If FileFieldSelected(FileField) Then
intFileLength = FileField.PropertyItems.Length
ReDim bytData(intFileLength)
objStream = ?????? 'Don't know what goes here
objStream.Read(bytData, 0, intFileLength)
Return bytData

With the input file, I have this: objStream =
FileField.PostedFile.InputStream

My suspicion is to save it like this: FileField.Save(objStream,
Imaging.ImageFormat.Jpeg)

Mar 15 '06 #8

P: n/a

<tj*****@phenom-biz.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
One more quick question -
I've done some researching and can't seem to find a straight answer to
this. In the following code, how do I actually save to the stream?
Here is what I have:
Dim intFileLength As Integer, bytData() As Byte
Dim objStream As System.IO.MemoryStream
'If FileFieldSelected(FileField) Then
intFileLength = FileField.PropertyItems.Length
ReDim bytData(intFileLength)
objStream = ?????? 'Don't know what goes here
objStream.Read(bytData, 0, intFileLength)
Return bytData

With the input file, I have this: objStream =
FileField.PostedFile.InputStream

My suspicion is to save it like this: FileField.Save(objStream,
Imaging.ImageFormat.Jpeg)


Actually, I just realized you don't even need a MemoryStream :)

I wrote this quick function to help out with this...

Private Function GetPostedFileBytes( _
ByVal PostedFileIndex As Integer _
) As Byte()
If Request.Files.Count >= (PostedFileIndex + 1)
Dim postedFile As HttpPostedFile = Request.Files(PostedFileIndex)
Dim bytes As Byte() = New Byte(postedFile.ContentLength - 1) { }

' Read the posted file.
postedFile.InputStream.Read(bytes, 0, bytes.Length)

' Return the byte-array.
Return bytes
Else
Return Nothing
End If
End Function

To use the function, pass in the index of the posted file you wish to
retrieve. The Request object contains a Files collection of HttpPostedFile
instances. Each instance represents a single posted file that was uploaded
by the user. If you only have a single <input type="file"/> object on the
page, then the index would be 0. It will return Nothing if there were no
requests or the index is invalid. If the file contains no data, it returns
an empty byte-array.

HTH :)

Mythran

Mar 15 '06 #9

P: n/a
That's where it gets tricky and thanks for the function, but I have
taken a posted file, created a new bitmap from the file and resized it
and then I want to get the info from the new bitmap. I can't seem to
get the byte-array from the bitmap. Currently, this is what I have:

Public Function GetByteArrayFromBitmap( _
ByVal FileField As System.Drawing.Bitmap) _
As Byte()
' Returns a byte array from the passed
' bitmap
Dim intFileLength As Integer
Dim objStream As System.IO.MemoryStream
FileField.Save(objStream, Imaging.ImageFormat.Jpeg)
Dim bytData(objStream.Length) As Byte
intFileLength = bytData.Length
objStream.Read(bytData, 0, intFileLength)
Return bytData
End Function

It doesn't work because my objStream is NULL. I get this error: Value
can't be null: Parameter name: stream.
I know I haven't done something right. I tried saving the new bitmap to
the original location, that worked, but when I upload the file to the
database, it's still the original copy of the file instead of the new
version. I think somehow the header info or something stays in memory
and when I continue to work with the input file control it references
the original information.

I really appreciate your help.

Mar 16 '06 #10

P: n/a

<tj*****@phenom-biz.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
That's where it gets tricky and thanks for the function, but I have
taken a posted file, created a new bitmap from the file and resized it
and then I want to get the info from the new bitmap. I can't seem to
get the byte-array from the bitmap. Currently, this is what I have:

Public Function GetByteArrayFromBitmap( _
ByVal FileField As System.Drawing.Bitmap) _
As Byte()
' Returns a byte array from the passed
' bitmap
Dim intFileLength As Integer
Dim objStream As System.IO.MemoryStream
FileField.Save(objStream, Imaging.ImageFormat.Jpeg)
Dim bytData(objStream.Length) As Byte
intFileLength = bytData.Length
objStream.Read(bytData, 0, intFileLength)
Return bytData
End Function

It doesn't work because my objStream is NULL. I get this error: Value
can't be null: Parameter name: stream.


Yup, you haven't instantiated an instance of the MemoryStream class...

objStream = New System.IO.MemoryStream()

So, your dim statement should look similar to:

Dim objStream As System.IO.MemoryStream = New System.IO.MemoryStream()

HTH :)

Mythran

Mar 16 '06 #11

P: n/a
You're right. I needed to do that, but even after I still had a few
issues. However, all is working now. So if others have this same
problem in the future, here is the abbreviated version of the code:

Dim intFileLength As Integer
Dim objStream As System.IO.MemoryStream = New
System.IO.MemoryStream
imgOutput.Save(objStream, Imaging.ImageFormat.Jpeg)
Dim bytData(objStream.Length) As Byte
intFileLength = bytData.Length
Dim intVar As Integer
bytData = objStream.GetBuffer
I had to use 'bytData = objStream.GetBuffer' rather than
'objStream.Read(bytData, 0, intFileLength)'

Mar 16 '06 #12

P: n/a

<tj*****@phenom-biz.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
You're right. I needed to do that, but even after I still had a few
issues. However, all is working now. So if others have this same
problem in the future, here is the abbreviated version of the code:

Dim intFileLength As Integer
Dim objStream As System.IO.MemoryStream = New
System.IO.MemoryStream
imgOutput.Save(objStream, Imaging.ImageFormat.Jpeg)
Dim bytData(objStream.Length) As Byte
intFileLength = bytData.Length
Dim intVar As Integer
bytData = objStream.GetBuffer
I had to use 'bytData = objStream.GetBuffer' rather than
'objStream.Read(bytData, 0, intFileLength)'


Your byte array is including an empty byte at the end of the array because
of how you are dimensioning it.

Dim bytData(...) AS Byte should be...

Dim bytData(objStream.Length - 1) As Byte

OR

Dim bytData As Byte() = New Byte(objStream.Length - 1) { }

:)

Cheers!

Mythran

Mar 17 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.