gdi +中发生了一般错误。保存图像文件

时间:2015-05-18 20:31:12

标签: asp.net vb.net image gdi+

我看到很多关于那个着名错误的帖子但是我无法修复它我试图保存2种不同类型的图像:1张图片的原始尺寸,2张基于原版的缩略图图片但是我遇到了到达那行代码时的特定错误:

ThumbnailBitmap.Save(ThumbnailImagePath)

原始图片:

  '# Check if path exist. (IRPictures)
                newFolder = ConfigurationManager.AppSettings("IRPictureDirPath").ToString()
                newFolder += m_User.CompanyCode
                newFolder += "/" + Year(IncidentDate).ToString()
                newFolder += "/" + Month(IncidentDate).ToString()
                newFolder += "/IR-" + incidentID.ToString()
                Dim di As DirectoryInfo = New DirectoryInfo((newFolder))

                '# Check if folder exists for pictures, if not: create it
                If Not Directory.Exists(newFolder) Then
                    Directory.CreateDirectory(newFolder)
                    If Convert.ToInt32(incidentNumberOfPictures.ToString()) > 0 Then

                        Dim dBConnection As String
                        dBConnection = Globals.GetConnectionStringMain(m_User.CompanyCode())
                        Dim connString As New SqlConnection(dBConnection)
                        For index = 1 To Convert.ToInt32(incidentNumberOfPictures.ToString())
                            Dim sql As String = "Select Picture,TakenOn from IncidentPictures where PictureIndex =" & index & " AND IncidentID=" + incidentID.ToString() & " order by PictureIndex"

                            Dim cmd As New SqlCommand(sql, connString)
                            If connString.State = ConnectionState.Closed Then
                                connString.Open()
                            End If
                            Dim dr As SqlDataReader = cmd.ExecuteReader()
                            If dr.HasRows Then
                                dr.Read()
                                Dim data As Byte() = DirectCast(dr("Picture"), Byte())
                                Dim TakenOn As DateTime = DirectCast(dr("TakenOn"), DateTime)
                                Dim ms As New MemoryStream(data)
                                Dim writer As New StreamWriter(ms)
                                'Dim strwOutput As New IO.StreamWriter(newFolder & "/" & incidentID.ToString() & "(" & index & ").jpg")
                                Dim strwOutput As New IO.StreamWriter(newFolder & "/" & "IR_" & incidentID.ToString() & "(" & TakenOn.ToString("yyyyMMddHHmmssfff") & ")" & "(" & index & ").jpg")
                                'After grabbing the picture we need to create a thumnail
                                testpath = newFolder & "/" & "IR_" & incidentID.ToString() & "(" & TakenOn.ToString("yyyyMMddHHmmssfff") & ")" & "(" & index & ").jpg"
                                ms.WriteTo(strwOutput.BaseStream)
                                strwOutput.Close()
                                ms.Close()
                                dr.Close()

                                '---------------- Resizing the image ----------------------------
                                CreateThumbnail(500, testpath, newThumbnailFolder)
                                ''---------------------------------------------------------------
                            End If
                        Next
                        connString.Close()
                    End If
                End If

缩略图:

Sub CreateThumbnail(ByVal ThumbnailMax As Integer, ByVal OriginalImagePath As String, ByVal ThumbnailImagePath As String)
        ' Loads original image from file
        Dim imgOriginal As Image = Image.FromFile(OriginalImagePath)
        ' Finds height and width of original image
        Dim OriginalHeight As Single = imgOriginal.Height
        Dim OriginalWidth As Single = imgOriginal.Width
        ' Finds height and width of resized image
        Dim ThumbnailWidth As Integer
        Dim ThumbnailHeight As Integer
        If OriginalHeight > OriginalWidth Then
            ThumbnailHeight = ThumbnailMax
            ThumbnailWidth = (OriginalWidth / OriginalHeight) * ThumbnailMax
        Else
            ThumbnailWidth = ThumbnailMax
            ThumbnailHeight = (OriginalHeight / OriginalWidth) * ThumbnailMax
        End If
        ' Create new bitmap that will be used for thumbnail
        Dim ThumbnailBitmap As Bitmap = New Bitmap(ThumbnailWidth, ThumbnailHeight)
        Dim ResizedImage As Graphics = Graphics.FromImage(ThumbnailBitmap)
        ' Resized image will have best possible quality
        ResizedImage.InterpolationMode = InterpolationMode.HighQualityBicubic
        ResizedImage.CompositingQuality = CompositingQuality.HighQuality
        ResizedImage.SmoothingMode = SmoothingMode.HighQuality
        ' Draw resized image
        ResizedImage.DrawImage(imgOriginal, 0, 0, ThumbnailWidth, ThumbnailHeight)
        ' Save thumbnail to file

        ThumbnailBitmap.Save(ThumbnailImagePath) '''''Error!!!!

    End Sub

0 个答案:

没有答案