|
-
Nov 30th, 2002, 01:24 AM
#1
Thread Starter
Junior Member
Resizing An Image in VB.NET
Well, I'm writing a Windows app to open up an image, store that into an SQL DB, resize it to a thumbnail size, and store that into the DB. I got everything to work, except for saving the image. First, just to test it, I .save'ed it to a system.io.file, instead of a system.io.stream (i need a stream to insert into the database). Here is my code, maybe I'm missing something...
Code:
Private Sub Go_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Go.Click
Dim origimg As System.Drawing.Image = System.Drawing.Image.FromFile(Image.Text)
Dim tsize As Size, dw As Int16, dh As Int16, thumbnail As Stream, bigimage As Stream
With origimg.Size
If .Width <= 320 Then dw = 1
If .Width > 320 And .Width <= 640 Then dw = 2
If .Width > 640 And .Width <= 960 Then dw = 3
If .Width > 960 And .Width <= 1024 Then dw = 4
If .Width > 1024 Then dw = 5
If .Height <= 320 Then dh = 1
If .Height > 320 And .Height <= 640 Then dh = 2
If .Height > 640 And .Height <= 960 Then dh = 3
If .Height > 960 And .Height <= 1024 Then dh = 4
If .Height > 1024 Then dh = 5
End With
tsize.Height = origimg.Size.Height / dh
tsize.Width = origimg.Size.Width / dw
'''''''''''''''''Errors on the following line
origimg.Save(bigimage, Imaging.ImageFormat.Jpeg)
Dim thumba As New System.Drawing.Bitmap(origimg, tsize.Width, tsize.Height)
thumba.Save(thumbnail, System.Drawing.Imaging.ImageFormat.Gif)
UpdateDB(Description.Text, bigimage, thumbnail, Category.SelectedText)
End Sub
The error it gives is: An unhandled exception of type 'System.NullReferenceException' occurred in DBImageUploader.exe, Additional information: Object reference not set to an instance of an object.
-
Nov 30th, 2002, 03:19 AM
#2
Member
I don't know anything about imaging with VB.NET yet but hopefully I can give you some help..
When it throws you that error click debug and it'll highlight the line with the error where the null reference is. Then move your cursor over each variable in that highlighted line to see what they are set to. Doing that you should easily enough be able to find which variable has the null reference and then be able to fix the problem (hopefully).
-
Nov 30th, 2002, 10:45 AM
#3
Thread Starter
Junior Member
No offense, I can tell you know nothing about Vb.Net :-). If you look in my cut/pasted code, i remarked the line that errored. Bigimage is dimmed as a System.IO.Stream, and is currently null (I want it to be filled with the image stream).
I just don't understand what I'm doing wrong.
-
Nov 30th, 2002, 12:22 PM
#4
Frenzied Member
Its null because no memory has be allocated for it, so its not pointing to anything.
Try this
VB Code:
Dim bigImage as New Stream()
Last edited by DevGrp; Dec 1st, 2002 at 04:35 AM.
Dont gain the world and lose your soul
-
Nov 30th, 2002, 02:29 PM
#5
Member
No offense, I can tell you know nothing about Vb.Net :-).
*shrug* No offense taken, sorry I tried to help.
-
Dec 1st, 2002, 01:19 AM
#6
Thread Starter
Junior Member
Thanks Guys!
Hehe, thanks for you help guys!
Wyrd -> Well, you helped keep this post of the top, so people with the answer could help :-).
DevGrp -> Thanks a lot!
-
Dec 1st, 2002, 03:24 PM
#7
Thread Starter
Junior Member
Well... I tried it, and well, it didnt work
I don't really understand how I would initialize it. Stream is a type, and not an object (i think), and vb won't let me declare it as new, or set it = new stream(). Also, I'm envoking the .Save command on origimg (which is a System.Drawing.Image), and trying to put it into a stream, so I can send it up to a database.
The help for the .Save command just shows to do this: obj.save(var dimmed as system.io.stream, Imaging.ImageFormat.Formatyouwant)
I can't envoke the stream.read(whatever), becuase origimg isn't a stream itself, or a list of bytes.
Can anyone shed some light on this one?
-
Dec 1st, 2002, 04:39 PM
#8
Member
Your problem is due to the IO.Stream class being an abstract class and shouldn't be used directly. You need to use another Stream object that inherits from IO.Stream. Looking at your code you'll more then likely need to use IO.MemoryStream, so;
bigimage = New IO.MemoryStream
I ran some minor tests and was able to save an image into the MemoryStream object (using the Length property to determine if information was indeed inside the MemoryStream object).
But like I said before, I don't know anything about Imaging or Streams in VB.NET quite yet but hopefully with the information I gave you, you can piece together a solution. 
Then again, according to you I know nothing about VB.NET so take what I say with a grain of salt.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|