タイトル | : Exceptionがcatchできない |
記事No | : 9702 |
投稿日 | : 2009/12/05(Sat) 09:18 |
投稿者 | : ダンボ |
皆さんこんにちは。 jpeg埋め込みサムネールを活用するのに、System.Drawing.ImageのGetThumbnailImage() メソッドを使っていましたが、今ひとつ速度が遅くて向上策を探していました。
http://www.vbforums.com/showthread.php?t=342386 試したところ、劇的に速くなりました。MrPoliteさん、どうもありがとうございます。 vb.net(VB2005)に直したコードは下記のGetThumbnailです。
扱っているjpeg画像にはサムネールが含まれないケースが少ないので、その場合だけ例外に した方が更に速度向上になると思い改修したのが、GetThumbnail_1とGetThumbnail_2ですが いずれもArgumentExceptionをcatchできません。何故? http://msdn.microsoft.com/ja-jp/library/system.drawing.image.getpropertyitem%28VS.80%29.aspx で言うように確かにArgumentExceptionは発生するのですがcatchできません。 (返信は遅れるかもしれませんがよろしくお願いします)
Private Function GetThumbnail(ByVal path As String) As Image Const THUMBNAIL_DATA As Integer = &H501B Dim p As Imaging.PropertyItem Using fs As IO.FileStream = IO.File.OpenRead(path) Using img As Image = Image.FromStream(fs, False, False) Dim propertyFound As Boolean = False For i As Integer = 0 To img.PropertyIdList.Length - 1 If img.PropertyIdList(i) = THUMBNAIL_DATA Then propertyFound = True Exit For End If Next If Not propertyFound Then Return Nothing End If p = img.GetPropertyItem(THUMBNAIL_DATA) fs.Close() End Using End Using
Dim imageBytes As Byte() = p.Value Using stream As New IO.MemoryStream(imageBytes.Length) stream.Write(imageBytes, 0, imageBytes.Length) Return Image.FromStream(stream) End Using End Function
(ArgumentExceptionがcatchできない) Private Function GetThumbnail_1(ByVal path As String) As Image Const THUMBNAIL_DATA As Integer = &H501B Dim p As Imaging.PropertyItem Try Using fs As IO.FileStream = IO.File.OpenRead(path) Using img As Image = Image.FromStream(fs, False, False) p = img.GetPropertyItem(THUMBNAIL_DATA) End Using fs.Close() End Using Dim imageBytes As Byte() = p.Value Using stream As New IO.MemoryStream(imageBytes.Length) stream.Write(imageBytes, 0, imageBytes.Length) Return Image.FromStream(stream) End Using Catch ex As Exception Return Nothing End Try End Function
Private Function GetThumbnail_2(ByVal path As String) As Image Const THUMBNAIL_DATA As Integer = &H501B Dim p As Imaging.PropertyItem Using fs As IO.FileStream = IO.File.OpenRead(path) Using img As Image = Image.FromStream(fs, False, False) Try p = img.GetPropertyItem(THUMBNAIL_DATA) Catch ex As System.ArgumentException Return Nothing Finally fs.Close() End Try End Using End Using Dim imageBytes As Byte() = p.Value Using stream As New IO.MemoryStream(imageBytes.Length) stream.Write(imageBytes, 0, imageBytes.Length) Return Image.FromStream(stream) End Using End Function
|