Last post Nov 24, 2009 02:00 PM by dotnetnoob
Nov 20, 2009 11:46 AM|dotnetnoob|LINK
Hi All: This is so basic a question I'm not sure where to ask it...but here goes.
I have a sub that takes a posted image file and resizes it, then saves it to a particular folder on the server. After the image manipulation is complete, I need to take the information about the new image (size, name, etc.) and store it in a SQL2005 table,
along with other information submitted in a form along with the posted file.
I currently do all of this in a single sub that's invoked by the form's submit button. I create a new System.Drawing.Image.Size object to hold resized image width/height, and then create a new System.Drawing.Bitmap that takes the posted file and the new
size constraints, then save the new bitmap in the correct imageFormat to disk. I then create a sql command to insert the information about the new image into a table, using the info in the Size object and the posted file name, as well as some other text fields
and values posted in the form.
All this works properly in a single sub, but I really need to separate the image resizing and storing in the dB so that I can invoke them independently. Sometimes I may want to edit just the existing dB info, or replace the existing image, or both, so it
seems like I should separate the image uploading from the dB update and call one or both depending on what's required. But, I'm stuck with how to get the new image sizing info back out of the sub, so I can pass it into the sub/function that updates the dB?
To do that, it seems like I would need to create a new "image object" that would hold all the information about the image (it's name, it's resized values, etc.), so that I can pass those parameters to the dB insert/update function...is that possible? Do
I need to put the resizing functionality in a class object? I'm lost on what the basic infrastructure should be for this.
Thanks for any suggestions!
Nov 23, 2009 11:52 AM|atconway|LINK
Yeah this is a good idea at this point to encapsulate the logic and properties of working with your custom images into thier own class. Below I have coded a simple class which expands on some of the properties you mentioned. You can easily change this
class or expand it by adding methods, etc. It is just a template to show you how to get this working. So 1st the 'Images' class:
''' Contains properties and methods to describe and hold an image
Public Class Images
Private mHeight As Integer = 0
Private mWidth As Integer = 0
Private mImageFile As Bitmap
Private mName As String
Public Sub New()
Public Property Height() As Integer
Set(ByVal value As Integer)
mHeight = value
Public Property Width() As Integer
Set(ByVal value As Integer)
mWidth = value
Public Property ImageFile() As Bitmap
Set(ByVal value As Bitmap)
mImageFile = value
Public Property Name() As String
Set(ByVal value As String)
mName = value
Now here is an example of instantiating the class above and settings its values. You of course would extract these values differently from the client, but it just shows you how to use the Images object:
'Create an instance of the 'Images' class to create a custom Image object
Dim MyImage As New Images()
'Add values to the Image (you would do this another way but it shows how to set values)
MyImage.Name = "Autum.jpg"
MyImage.Height = 600
MyImage.Width = 800
MyImage.ImageFile = New System.Drawing.Bitmap("C:\Autum.jpg")
And lastly, below is a sample method that takes an 'Images' object as a parameter. This is ultimately what you will want to do by building up an instance and passing the object around rather than the individual parameters. The method is simple and you could easily make your own or expand on it as needed:
Public Sub SendImageToDB(ByVal ImageObj As Images)
'Do whatever you need at this point. You have passed an object instance
'to this method, and now you can use its argument values to call the db for any functions needed
Dim cmd As New System.Data.SqlClient.SqlCommand
'Add values to SQLCommand object
Hope this helps!
Nov 24, 2009 02:00 PM|dotnetnoob|LINK
That's perfect! Thanks so much for taking the time to code it out for me...now it all makes sense!