Answered by:
Compress varbinary photo prior to inserting?

Question
-
User-373571878 posted
Hello,
Thanks to Kedar Kulkarni I am now able to store a binary photo in my sql database and retrieve the photos.
Is there a way I can compress these photos prior to inserting them into the data base?
The insert code looks like:
'new code to create byte data from photourl string Dim stream As New FileStream(PhotoUrl, FileMode.Open) Dim ImgInfo As New FileInfo(PhotoUrl) Dim length As Integer = Convert.ToInt32(ImgInfo.Length) 'Creating byte array to pass as parameter photo = New Byte(length - 1) {} 'fill byte array stream.Read(photo, 0, length)
Photo is the new binary image.
Monday, June 27, 2011 2:54 PM
Answers
-
User551462331 posted
ok... create two seprate function in your vb file like this
the first two parameters to method GetThumbnailImage in below function is width and height of output thumbnail image... change it as required
Private function ConvertToThumbNail(ByVal strOriginalImagePath As string,ByVal strNewImagePath As string, ByVal strImagename As string ) as string Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback) Dim myBitmap As New Bitmap(strOriginalImagePath ) Dim myThumbnail As Image = myBitmap.GetThumbnailImage(40, 40, myCallback, IntPtr.Zero) myThumbnail.Save(strNewImagePath & "/" & strImagename & "_Thumbnail.jpg") return strNewImagePath & "/" & strImagename & "_Thumbnail.jpg" End Sub Public Function ThumbnailCallback() As Boolean Return False End Function
now, you just need to call this function at following location in u r code..... note code in bold....
'**************************** photo upload start Dim Filename As String = "" Dim SendFromServerFolder As String = "" Dim photo As Byte() = {0} If FileUpload1.HasFile Then Filename = UCase(FileUpload1.FileName) Filename = Mid(Filename, InStr(Filename, ".")) If Filename = ".JPG" Or _ Filename = ".JPEG" Or _ Filename = ".GIF" Then 'let pass Else lblInfo.Text = "Invalid File Type!" Me.lblInfo.ForeColor = Drawing.Color.Red Exit Sub End If ' save uploaded photo to file on web server ...may change eventually Try FileUpload1.SaveAs(ConfigurationManager.AppSettings("PhotoUrl") & _ FileUpload1.FileName) Catch ex As Exception lblInfo.Text = "ERROR: " & ex.Message.ToString() Me.lblInfo.ForeColor = Drawing.Color.Red Exit Sub End Try Else End If If FileUpload1.FileName = "" Then PhotoUrl = ConfigurationManager.AppSettings("PhotoUrl") & "nophotogeneric.gif" Me.Session("PhotoUrl") = PhotoUrl Try Dim stream As New FileStream(PhotoUrl, FileMode.Open) Dim ImgInfo As New FileInfo(PhotoUrl) Dim length As Integer = Convert.ToInt32(ImgInfo.Length) 'Creating byte array to pass as parameter photo = New Byte(length - 1) {} 'fill byte array stream.Read(photo, 0, length) Catch ex As Exception Throw ex End Try Else SendFromServerFolder = ConfigurationManager.AppSettings("PhotoUrl") & FileUpload1.FileName Me.Image1.ImageUrl = ConvertToThumnail(SendFromServerFolder, ConfigurationManager.AppSettings("PhotoUrl"),FileUpload1.FileName.Split(".")(0) ) PhotoUrl = ConvertToThumnail(SendFromServerFolder, ConfigurationManager.AppSettings("PhotoUrl"),FileUpload1.FileName.Split(".")(0) ) Me.Session("PhotoUrl") = PhotoUrl Dim photolength As Integer photolength = FileUpload1.FileName.Length Dim imageBytes(FileUpload1.FileName.Length) As Byte PhotoUrl = PhotoUrl 'new code to create byte data from photourl string Dim stream As New FileStream(PhotoUrl, FileMode.Open) Dim ImgInfo As New FileInfo(PhotoUrl) Dim length As Integer = Convert.ToInt32(ImgInfo.Length) 'Creating byte array to pass as parameter photo = New Byte(length - 1) {} 'fill byte array stream.Read(photo, 0, length) End If '**************************** photo upload end
what i have done here is... using new function, i m getting thumbnail image created of original image
thumbnaile image should be created in the same location where original image is uploaded...
the thumbnail image will have name as _thumbnail appended in orginal image file...
i.e. if original file name is abc.jpg then thumbnail image will be created in same folder with name as abc_thumbnail.jpg
after that, path of newly created thumbnail image only used to convert into byte... so compressed image will be stored in database...
let me know if any issue...
hope this helps....
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 1, 2011 12:44 PM
All replies
-
User551462331 posted
what is type of image file u will upload..... unless bmp, there is no big advantage of compressing image file....
compressd file size will not be too less than original (like txt files)
u can read below...
http://www.codeproject.com/KB/database/blob_compress.aspx
http://geekswithblogs.net/bullpit/archive/2009/04/29/compress-image-files-using-c.aspx
hope this helps...
Monday, June 27, 2011 3:35 PM -
User-373571878 posted
the image type is .jpg.
When I compress the images prior to uploading the photos load pretty fast. If I do not they take some time to display.
I edit the photos in Microsoft Picture Manager, I then choose compress pictures..and 'Compress for:' Web pages'.
I was hoping I could compress the images prior to inserting them into the database.
If I can not compress, using the code is there a way to bring the photo in as a thumb nail?
Thank you.
Monday, June 27, 2011 3:55 PM -
User551462331 posted
to get thumbnail, there is method of image class GetThumbNailImage like this
Image img = Image.ImageFromFile("imagename.jpg"); Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback); Image thumbnail = myBitmap.GetThumbnailImage(40, 40, myCallback, IntPtr.Zero); thumbnail.save("thumbnail.jpg");
refer this for more details
http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx
hope this helps...
Monday, June 27, 2011 4:20 PM -
User1508394307 posted
Set lower quality, or resize to a smaller size. See example here:
http://www.geekpedia.com/tutorial232_Reducing-JPEG-Picture-Quality-using-Csharp.html
Monday, June 27, 2011 4:58 PM -
User-373571878 posted
Thanks for the link.
I'm not sure if this will work vs 2008 as the example they show is 4.0 (?) and when I select another framework version no code is supplied?
I included the system.drawing name space but I'm getting a compiler error PaintEventArgs stating the type is not defined.
If this is worth giving a shot, how would I combine the code below (which works) with the get thumbnail logic?
'new code to create byte data from photourl string Dim stream As New FileStream(PhotoUrl, FileMode.Open) Dim ImgInfo As New FileInfo(PhotoUrl) Dim length As Integer = Convert.ToInt32(ImgInfo.Length) 'Creating byte array to pass as parameter photo = New Byte(length - 1) {} 'fill byte array stream.Read(photo, 0, length)
Tuesday, June 28, 2011 10:53 AM -
User551462331 posted
try to use below code to get thumbnail image...
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback) Dim myBitmap As New Bitmap(Server.MapPath("Images") & "/old.jpg") Dim myThumbnail As Image = myBitmap.GetThumbnailImage(40, 40, myCallback, IntPtr.Zero) myThumbnail.Save(Server.MapPath("Images") & "/new.jpg") End Sub Public Function ThumbnailCallback() As Boolean Return False End Function
i used button click event to test the code... u can use this code in separate function or event...
u have to change name of image old.jpg and new.jpg and also image path might be diffrent in u r case...
let me know if it works for u...
hope this helps...
Tuesday, June 28, 2011 3:32 PM -
User-373571878 posted
Thanks KK
What is "AddressOf ThumbnailCallback"? And the /Old.Jpg and /New.jpg?
Wednesday, June 29, 2011 10:02 AM -
User551462331 posted
if u look at the code i posted... we have created a separate function ThumbnailCallback..
addressof ThumbnailCallback is setting the delegate.. method of Bitmap.GetThumbnailImage accepts this delegate as parametr..
this delegate is supposed to return true if you want to stop execution of method GetThumbnailImage else it should retun false.
read this for more information..
http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx
and for more about delegate getthumbnailimageabort
http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimageabort.aspx
in my code.. old.jpg is the name of image file present in the image folder (under root folder there is image folder and old.jpg in that)
new.jpg is the name of the modified image (thumbnail image) from old.jpg
set some jpg image file in u r folder and replace old.jpg with that file name... and run the code... after execution of this code... u can see new image file in same folder with name new.jpg (u can then change name of new.jpg as appropriate)
let me know if it is working for u
hope this helps...
Wednesday, June 29, 2011 10:53 AM -
User-373571878 posted
Sorry Kedar,
I got pulled away from this yesterday.
I'm still a little confused as to how I will fit this in with the flow of uploading and inserting a photo.
I have an insert form and on there is an file upload control.
The code below gets the photo url (which I save) and also converts the photo to binary which I also save into the table.
Where I get confused is, if I elect to save a thumbnail instead of the original photo how and where do I fit the new thumbnail logic in?
'**************************** photo upload start Dim Filename As String = "" Dim SendFromServerFolder As String = "" Dim photo As Byte() = {0} If FileUpload1.HasFile Then Filename = UCase(FileUpload1.FileName) Filename = Mid(Filename, InStr(Filename, ".")) If Filename = ".JPG" Or _ Filename = ".JPEG" Or _ Filename = ".GIF" Then 'let pass Else lblInfo.Text = "Invalid File Type!" Me.lblInfo.ForeColor = Drawing.Color.Red Exit Sub End If ' save uploaded photo to file on web server ...may change eventually Try FileUpload1.SaveAs(ConfigurationManager.AppSettings("PhotoUrl") & _ FileUpload1.FileName) Catch ex As Exception lblInfo.Text = "ERROR: " & ex.Message.ToString() Me.lblInfo.ForeColor = Drawing.Color.Red Exit Sub End Try Else End If If FileUpload1.FileName = "" Then PhotoUrl = ConfigurationManager.AppSettings("PhotoUrl") & "nophotogeneric.gif" Me.Session("PhotoUrl") = PhotoUrl Try Dim stream As New FileStream(PhotoUrl, FileMode.Open) Dim ImgInfo As New FileInfo(PhotoUrl) Dim length As Integer = Convert.ToInt32(ImgInfo.Length) 'Creating byte array to pass as parameter photo = New Byte(length - 1) {} 'fill byte array stream.Read(photo, 0, length) Catch ex As Exception Throw ex End Try Else SendFromServerFolder = ConfigurationManager.AppSettings("PhotoUrl") & FileUpload1.FileName Me.Image1.ImageUrl = SendFromServerFolder PhotoUrl = ConfigurationManager.AppSettings("PhotoUrl") & FileUpload1.FileName Me.Session("PhotoUrl") = PhotoUrl Dim photolength As Integer photolength = FileUpload1.FileName.Length Dim imageBytes(FileUpload1.FileName.Length) As Byte PhotoUrl = PhotoUrl 'new code to create byte data from photourl string Dim stream As New FileStream(PhotoUrl, FileMode.Open) Dim ImgInfo As New FileInfo(PhotoUrl) Dim length As Integer = Convert.ToInt32(ImgInfo.Length) 'Creating byte array to pass as parameter photo = New Byte(length - 1) {} 'fill byte array stream.Read(photo, 0, length) End If '**************************** photo upload end
the field 'Photo' now contains the binary representation of the photo.
Friday, July 1, 2011 10:15 AM -
User551462331 posted
ok... create two seprate function in your vb file like this
the first two parameters to method GetThumbnailImage in below function is width and height of output thumbnail image... change it as required
Private function ConvertToThumbNail(ByVal strOriginalImagePath As string,ByVal strNewImagePath As string, ByVal strImagename As string ) as string Dim myCallback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback) Dim myBitmap As New Bitmap(strOriginalImagePath ) Dim myThumbnail As Image = myBitmap.GetThumbnailImage(40, 40, myCallback, IntPtr.Zero) myThumbnail.Save(strNewImagePath & "/" & strImagename & "_Thumbnail.jpg") return strNewImagePath & "/" & strImagename & "_Thumbnail.jpg" End Sub Public Function ThumbnailCallback() As Boolean Return False End Function
now, you just need to call this function at following location in u r code..... note code in bold....
'**************************** photo upload start Dim Filename As String = "" Dim SendFromServerFolder As String = "" Dim photo As Byte() = {0} If FileUpload1.HasFile Then Filename = UCase(FileUpload1.FileName) Filename = Mid(Filename, InStr(Filename, ".")) If Filename = ".JPG" Or _ Filename = ".JPEG" Or _ Filename = ".GIF" Then 'let pass Else lblInfo.Text = "Invalid File Type!" Me.lblInfo.ForeColor = Drawing.Color.Red Exit Sub End If ' save uploaded photo to file on web server ...may change eventually Try FileUpload1.SaveAs(ConfigurationManager.AppSettings("PhotoUrl") & _ FileUpload1.FileName) Catch ex As Exception lblInfo.Text = "ERROR: " & ex.Message.ToString() Me.lblInfo.ForeColor = Drawing.Color.Red Exit Sub End Try Else End If If FileUpload1.FileName = "" Then PhotoUrl = ConfigurationManager.AppSettings("PhotoUrl") & "nophotogeneric.gif" Me.Session("PhotoUrl") = PhotoUrl Try Dim stream As New FileStream(PhotoUrl, FileMode.Open) Dim ImgInfo As New FileInfo(PhotoUrl) Dim length As Integer = Convert.ToInt32(ImgInfo.Length) 'Creating byte array to pass as parameter photo = New Byte(length - 1) {} 'fill byte array stream.Read(photo, 0, length) Catch ex As Exception Throw ex End Try Else SendFromServerFolder = ConfigurationManager.AppSettings("PhotoUrl") & FileUpload1.FileName Me.Image1.ImageUrl = ConvertToThumnail(SendFromServerFolder, ConfigurationManager.AppSettings("PhotoUrl"),FileUpload1.FileName.Split(".")(0) ) PhotoUrl = ConvertToThumnail(SendFromServerFolder, ConfigurationManager.AppSettings("PhotoUrl"),FileUpload1.FileName.Split(".")(0) ) Me.Session("PhotoUrl") = PhotoUrl Dim photolength As Integer photolength = FileUpload1.FileName.Length Dim imageBytes(FileUpload1.FileName.Length) As Byte PhotoUrl = PhotoUrl 'new code to create byte data from photourl string Dim stream As New FileStream(PhotoUrl, FileMode.Open) Dim ImgInfo As New FileInfo(PhotoUrl) Dim length As Integer = Convert.ToInt32(ImgInfo.Length) 'Creating byte array to pass as parameter photo = New Byte(length - 1) {} 'fill byte array stream.Read(photo, 0, length) End If '**************************** photo upload end
what i have done here is... using new function, i m getting thumbnail image created of original image
thumbnaile image should be created in the same location where original image is uploaded...
the thumbnail image will have name as _thumbnail appended in orginal image file...
i.e. if original file name is abc.jpg then thumbnail image will be created in same folder with name as abc_thumbnail.jpg
after that, path of newly created thumbnail image only used to convert into byte... so compressed image will be stored in database...
let me know if any issue...
hope this helps....
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 1, 2011 12:44 PM