473,513 Members | 13,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to resize an image in a memoryStream

62 New Member
I wrote a piece of code that grabs a fileupload, puts it into a memory stream, converts the main pic, and thumbnail pic, then saves them as jpegs, then updates the database.

Everything works fine, except one little glitch. If the DPI is above 300 then the image doesn't save right. It get's pixelated and blury. If anyone knows what is doing this, or how to solve it, I'd greatly appreciate the help.

here is the code.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4.         Dim fileLen As Integer
  5.         Dim myStream As System.IO.Stream
  6.         Dim ImgContentType As String
  7.         Dim UserName = Dal.GetNickName(Dal.GetUserID)
  8.  
  9.         Dim FileUploadPicture As FileUpload = SuperFormAquariums.FindControl("FileUploadEditPicture")
  10.  
  11.         'Response.Write(FileUploadPicture.HasFile)
  12.         'Response.End()
  13.  
  14.         If FileUploadPicture.HasFile Then
  15.             'Create Dir
  16.             Dim nd As New DirectoryInfo(Server.MapPath("\" + Application("SysUserDir") & "\" & UserName & "\Aquariums\Picture\"))
  17.             If nd.Exists = False Then
  18.                 nd.Create()
  19.             End If
  20.  
  21.             'Create Dir
  22.             Dim nd2 As New DirectoryInfo(Server.MapPath("\" + Application("SysUserDir") & "\" & UserName & "\Aquariums\PictureThumb\"))
  23.             If nd2.Exists = False Then
  24.                 nd2.Create()
  25.             End If
  26.  
  27.  
  28.  
  29.             fileLen = FileUploadPicture.PostedFile.ContentLength
  30.             ImgContentType = FileUploadPicture.PostedFile.ContentType
  31.  
  32.             Dim Input(fileLen) As Byte
  33.             myStream = FileUploadPicture.PostedFile.InputStream
  34.             myStream.Read(Input, 0, fileLen)
  35.  
  36.             'Response.Write(fileLen)
  37.             'Response.End()
  38.  
  39.             Dim Filename = FileUploadPicture.PostedFile.FileName
  40.  
  41.             Dim Path1 = Server.MapPath("\" + Application("SysUserDir") & "\" & UserName & "\Aquariums\Picture\" & Filename)
  42.             Dim DataPath1 = "/" & UserName & "/Aquariums/Picture/" & Filename
  43.             Dim OldPath1 = Server.MapPath("/" & Application("SysUserDir") & "/" & e.OldValues("Picture"))
  44.  
  45.             Dim Path2 = Server.MapPath("\" + Application("SysUserDir") & "\" & UserName & "\Aquariums\PictureThumb\" & Filename)
  46.             Dim DataPath2 = "/" & UserName & "/Aquariums/PictureThumb/" & Filename
  47.             Dim OldPath2 = Server.MapPath("/" & Application("SysUserDir") & "/" & e.OldValues("PictureThumb"))
  48.  
  49.  
  50.             'Response.Write(DataPath1 & " " & DataPath2)
  51.             'Response.End()
  52.             'delete old image
  53.  
  54.             'delete old files
  55.             If FileUploadPicture.HasFile Then
  56.                 Dim fd1 As New FileInfo(OldPath1)
  57.                 If fd1.Exists = True Then
  58.                     fd1.Delete()
  59.                 End If
  60.             End If
  61.  
  62.             If FileUploadPicture.HasFile Then
  63.                 Dim fd2 As New FileInfo(OldPath2)
  64.                 If fd2.Exists = True Then
  65.                     fd2.Delete()
  66.                 End If
  67.             End If
  68.  
  69.  
  70.  
  71.  
  72.  
  73.             If File.Exists(Path1) Or File.Exists(Path2) Then
  74.  
  75.                 'LabelPhotos.Text = "Photo has not been added to the system"
  76.                 LabelConfirm.Text = "This photo image already exists in the system"
  77.                 e.Cancel = True
  78.  
  79.             Else
  80.  
  81.  
  82.  
  83.                 LabelConfirm.Text = ""
  84.                 Dim PictureInfoLarge = System.Drawing.Image.FromStream(myStream)
  85.  
  86.  
  87.  
  88.  
  89.                 'Get Picture To be Resized large
  90.                 Dim maxWidth1 As Integer = 800
  91.                 Dim maxHeight1 As Integer = 800
  92.                 Dim imgHeight1, imgWidth1 As Integer
  93.                 'Dim PictureInfo
  94.  
  95.  
  96.  
  97.                 imgHeight1 = PictureInfoLarge.Height
  98.                 imgWidth1 = PictureInfoLarge.Width
  99.  
  100.  
  101.                 ' Determine Scaling
  102.                 If imgWidth1 >= maxWidth1 Or imgHeight1 >= maxHeight1 Then
  103.                     'Determine what dimension is off by more
  104.                     Dim deltaWidth1 As Integer = imgWidth1 - maxWidth1
  105.                     Dim deltaHeight1 As Integer = imgHeight1 - maxHeight1
  106.                     Dim scaleFactor1 As Double
  107.  
  108.                     If deltaHeight1 > deltaWidth1 Then
  109.                         'Scale by the height
  110.                         scaleFactor1 = maxHeight1 / imgHeight1
  111.                     Else
  112.                         'Scale by the Width
  113.                         scaleFactor1 = maxWidth1 / imgWidth1
  114.                     End If
  115.  
  116.                     imgWidth1 *= scaleFactor1
  117.                     imgHeight1 *= scaleFactor1
  118.  
  119.                 Else
  120.  
  121.  
  122.                 End If
  123.  
  124.                 'Dim objThumbnail As System.Drawing.Image = PictureInfoLarge.GetThumbnailImage(imgWidth1, imgHeight1, Nothing, System.IntPtr.Zero)
  125.                 Dim objThumbnail As System.Drawing.Bitmap = PictureInfoLarge.GetThumbnailImage(imgWidth1, imgHeight1, Nothing, System.IntPtr.Zero)
  126.                 ' Dim graphicsImage As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(objThumbnail)
  127.  
  128.  'this is where the glitch is I think---------------supposed to fix -----------------------------------
  129.                 Dim graphicsImage As Graphics
  130.                 graphicsImage = Graphics.FromImage(objThumbnail)
  131.                 graphicsImage.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
  132.                 graphicsImage.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
  133.                 graphicsImage.DrawImage(objThumbnail, 0, 0, imgWidth1, imgHeight1)
  134.                 '--------------------------------------------------------------------
  135.                 graphicsImage.Save()
  136.  
  137.                 objThumbnail.Save(Path1, Imaging.ImageFormat.Jpeg)
  138.  
  139.                 'Response.Write(objThumbnail.PixelFormat & "<BR><BR>")
  140.                 'Response.Write(objThumbnail.PhysicalDimension)
  141.                 'Response.Write(objThumbnail.Size)
  142.  
  143.                 'Response.End()
  144.  
  145.  
  146.                 graphicsImage.Dispose()
  147.  
  148.                 e.NewValues("Picture") = DataPath1
  149.  
  150.                 PictureInfoLarge = Nothing
  151.                 objThumbnail = Nothing
  152.  
  153.  
  154.                 Dim PictureInfoSmall = System.Drawing.Image.FromStream(myStream)
  155.  
  156.  
  157.                 'Get Picture To be Resized small
  158.                 Dim maxWidth2 = 150
  159.                 Dim maxHeight2 = 150
  160.                 'imgHeight, imgWidth As Integer
  161.                 'Dim PictureInfo
  162.  
  163.  
  164.  
  165.                 Dim imgHeight2 = PictureInfoSmall.Height
  166.                 Dim imgWidth2 = PictureInfoSmall.Width
  167.  
  168.                 ' Determine Scaling
  169.                 If imgWidth2 >= maxWidth2 Or imgHeight2 >= maxHeight2 Then
  170.                     'Determine what dimension is off by more
  171.                     ' Dim deltaWidth As Integer = imgWidth - maxWidth
  172.                     'Dim deltaHeight As Integer = imgHeight - maxHeight
  173.                     ' Dim scaleFactor As Double
  174.                     Dim deltaWidth2 As Integer = imgWidth2 - maxWidth2
  175.                     Dim deltaHeight2 As Integer = imgHeight2 - maxHeight2
  176.                     Dim scaleFactor2 As Double
  177.  
  178.                     If deltaHeight2 > deltaWidth2 Then
  179.                         'Scale by the height
  180.                         scaleFactor2 = maxHeight2 / imgHeight2
  181.                     Else
  182.                         'Scale by the Width
  183.                         scaleFactor2 = maxWidth2 / imgWidth2
  184.                     End If
  185.  
  186.                     imgWidth2 *= scaleFactor2
  187.                     imgHeight2 *= scaleFactor2
  188.  
  189.                 Else
  190.  
  191.                 End If
  192.  
  193.  
  194.                 Dim objThumbnail2 As System.Drawing.Bitmap = PictureInfoSmall.GetThumbnailImage(imgWidth2, imgHeight2, Nothing, System.IntPtr.Zero)
  195.  
  196.                 '---------------supposed to fix -----------------------------------
  197.                 Dim graphicsImage2 As Graphics
  198.                 graphicsImage2 = Graphics.FromImage(objThumbnail2)
  199.                 graphicsImage2.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
  200.                 graphicsImage2.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
  201.                 graphicsImage2.DrawImage(objThumbnail2, 0, 0, imgWidth1, imgHeight1)
  202.                 '--------------------------------------------------------------------
  203.                 graphicsImage2.Save()
  204.  
  205.                 objThumbnail2.Save(Path2, Imaging.ImageFormat.Jpeg)
  206.  
  207.                 graphicsImage2.Dispose()
  208.  
  209.  
  210.                 e.NewValues("PictureThumb") = DataPath2
  211.  
  212.  
  213.                 myStream.Close()
  214.                 myStream.Dispose()
  215.  
  216.                 PictureInfoSmall = Nothing
  217.                 objThumbnail2 = Nothing
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.             End If
  226.  
  227.  
  228.         ElseIf Not FileUploadPicture.HasFile Then
  229.  
  230.             'Dim PictureOld = CType(SuperFormAquariums.FindControl("HiddenFieldPictureEdit"), HiddenField).Value
  231.             'Dim PictureThumbOld = CType(SuperFormAquariums.FindControl("HiddenFieldPictureThumbedit"), HiddenField).Value
  232.             'Response.Write("new:" & e.NewValues("Picture") & " " & e.NewValues("PictureThumb") & " OLD: " & e.OldValues("Picture") & " " & e.OldValues("PictureThumb"))
  233.             'Response.End()
  234.  
  235.             e.NewValues("Picture") = e.OldValues("Picture")
  236.  
  237.             e.NewValues("PictureThumb") = e.OldValues("PictureThumb")
  238.  
  239.         End If
  240.  
regards,

vertigo
Oct 1 '10 #1
1 1768
vertigo262
62 New Member
just caught this glitch

# Dim objThumbnail2 As System.Drawing.Bitmap = PictureInfoSmall.GetThumbnailImage(imgWidth2, imgHeight2, Nothing, System.IntPtr.Zero)
#
# '---------------supposed to fix -----------------------------------
# Dim graphicsImage2 As Graphics
# graphicsImage2 = Graphics.FromImage(objThumbnail2)
# graphicsImage2.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
# graphicsImage2.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
# graphicsImage2.DrawImage(objThumbnail2, 0, 0, imgWidth1, imgHeight1)
# '--------------------------------------------------------------------
# graphicsImage2.Save()



should be

imgWidth2, imgHeight2 instead of imgWidth1, imgHeight1


but that's not the problem
Oct 8 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
14461
by: Julia Briggs | last post by:
I am struggling to create a PHP function that would take a specified image (JPG, GIF or PNG) from a link, and resize it down to a thumbnail so it will always fit in a 200x250 space. I am hoping...
1
4343
by: John Scott | last post by:
I am storing an image in an SQL database and have one field as an image datatype. I am also using a webservice to transport data. I want to be able to resize the image and pass back a thumbnail...
1
1888
by: David | last post by:
Hello. How can I resize image in Bitmap object?
3
2819
by: DichkoSoft | last post by:
Dear All I don't know haw i resize inage in VB.NET I want load image with size 800x600 then rezize it to 80x60 pixels and save to a file. -- DichkoSoft
2
1637
by: Adam J Knight | last post by:
Hi all, I am trying resize an image using the following code. //resize image System.Drawing.Image ImageUpload =...
1
3747
by: Arjen | last post by:
Hi, I want to resize an image on my server. I tried a lot of samples... but all the time it does resize and saves the images but I can not view the image insize a webbrowser. With an...
6
4499
by: Bob Bedford | last post by:
Hi all, I've a classic upload form where people may select images on their computer to send on my website. Since this is done by people not very confortable with computers, some sent pictures...
6
3311
by: Bob Bedford | last post by:
Hi all, I've to resize uploaded images with the "imagecopyresampled" but when I've images quite large (common those days) I reach the 16mb limits of the ISP. How can I fix this ? I absolutely...
3
14069
by: Danny Ni | last post by:
Hi, I am looking for a way to display images with different aspect ratio into frames with fixed width and height, the problem is some images will look distorted if they are forced into fixed...
3
2548
by: =?Utf-8?B?cGVsZWdrMQ==?= | last post by:
how can i reesize an image so its quallity will stay the same as the original 1? thnaks in advance peleg
0
7254
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
7153
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
7373
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
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.