ファイルからバイト配列を取得する。
Dim bytes() As Byte
Using fs As New System.IO.FileStream("パス", IO.FileMode.Open, IO.FileAccess.Read)
Using br As New System.IO.BinaryReader(fs)
bytes = br.ReadBytes(CInt(fs.Length))
br.Close()
End Using
fs.Close()
End UsingファイルからImageオブジェクトを取得する。
Dim image As System.Drawing.Image
Using fs As New System.IO.FileStream("パス", System.IO.FileMode.Open)
image = System.Drawing.Image.FromStream(fs)
fs.Close()
End Usingバイト配列からImageオブジェクトを取得する。
Dim bytes() as Byte = 画像のバイト配列 Dim image As System.Drawing.Image Usind ms As New System.IO.MemoryStream(bytes) image = System.Drawing.Image.FromStream(ms) ms.Close() End Using
ピクチャーボックスに表示された画像のバイト配列を取得する。
Using ms As New System.IO.MemoryStream() Me.PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp) Dim bytes(Cint(ms.Length)) As Byte ms.Position = 0 ms.Read(bytes, 0, Convert.ToInt32(ms.Length)) ms.Close() End Using
上記の「ファイルからImageオブジェクトを取得する」や「バイト配列からImageオブジェクトを取得する」で取得したImageオブジェクトをPictureBoxのImageプロパティにそのまま設定すると、「ピクチャーボックスに表示された画像のバイト配列を取得する」のImage.SaveでGDI+汎用エラーになってしまいます。
この問題を回避するには、取得したImageオブジェクトからBitmapオブジェクトを作成してPictureBoxのImageプロパティに設定します。
Me.PictureBox1.Image = New System.Drawing.Bitmap(取得したImageオブジェクト)
0 件のコメント:
コメントを投稿