作成日: 2024/09/13 最終更新日: 2024/09/13
文書種別
不具合
状況
回避方法あり
詳細
GcBarCodeコントロールのValueプロパティに不正な値を設定しtry-catchで例外を処理します。この時、CreateBitmapメソッドの引数にDPIを指定している場合は例外をキャッチできますがSizeを指定した時には例外をキャッチできません。例えば、以下のようなコードで本現象を確認できます。
◎サンプルコード(VB)
Imports GrapeCity.Win.BarCode
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 作成したバーコード画像の表示
PictureBox1.Image = CreateCode39()
End Sub
Private Function CreateCode39()
Dim image As Image = Nothing
Dim code As String = "あいうえお" ' 例外を発生させるためのテスト用の値
Try
' Code39の作成
Dim barCode As New GcBarCode With {
.Type = ValueType.BarType.Code39,
.CheckDigit = True,
.Message = True,
.MessagePos = ValueType.MessagePos.Default,
.Value = code
}
' 画像の生成
'image = barCode.CreateBitmap(Me.DeviceDpi) ' 例外をキャッチできる
image = barCode.CreateBitmap(New Size(240, 100)) ' 例外をキャッチできない
Catch ex As BarCodeException
MessageBox.Show(ex.Message, "Code39")
End Try
' 戻り値の設定
Return image
End Function
End Class
回避方法
例外が発生しないようにカスタマイズした以下のCreateBitmapExメソッドを使用して画像を生成することで回避できます。
◎サンプルコード(VB)
Imports GrapeCity.Win.BarCode
Imports System.Runtime.CompilerServices
Imports System.Runtime.InteropServices
Module GcBarCodeExtension
<DllImport("user32.dll")>
Private Function SendMessage(hWnd As IntPtr, Msg As UInteger, wParam As IntPtr, lParam As IntPtr) As IntPtr
End Function
<Extension()>
Public Function CreateBitmapEx(ByVal gcBarCode As GcBarCode, ByVal size As Size) As Image
Dim oldSize As Size = gcBarCode.Size
If gcBarCode.IsHandleCreated Then
SendMessage(gcBarCode.Handle, &HB, IntPtr.Zero, IntPtr.Zero)
End If
gcBarCode.Size = size
Dim image As Image = gcBarCode.CreateMetafile()
Dim bmp As New Bitmap(image)
gcBarCode.Size = oldSize
If gcBarCode.IsHandleCreated Then
SendMessage(gcBarCode.Handle, &HB, New IntPtr(1), IntPtr.Zero)
End If
Dim bitmap As Bitmap = bmp.Clone(New Rectangle(0, 0, bmp.Width - 1, bmp.Height - 1), System.Drawing.Imaging.PixelFormat.Format24bppRgb)
bmp.Dispose()
image.Dispose()
Return bitmap
End Function
End Module
画像の生成処理を上記のメソッドに書き換えます。
image = barCode.CreateBitmapEx(New Size(240, 100))