VB2005+SQLServer2005で、Form に貼られている PictureBox の
画像(Bitmai)を、SQLServer の IMAGE 型に更新させる方法を
調べています。
Byte 型の配列から SQLServer に更新させる方法は
なんとなくわかったのですが、 PictureBox の画像を
Byte 型の配列に入れる方法が良くわかりません。
ご存知の方お願いします。
> Form に貼られている PictureBox の画像(Bitmai)を
Bitmai → Bitmap の事だと思いますが、それならば、こんな感じ。
Public Shared Function GetBinary(ByVal bmp As Bitmap) As Byte()
Using stm As New System.IO.MemoryStream()
bmp.Save(stm, Imaging.ImageFormat.Bmp)
stm.Seek(0, IO.SeekOrigin.Begin)
Return stm.ToArray()
End Using
End Function
失礼しました。
DB に格納することを考えると、Bmp 形式だと冗長すぎますね。
Png の方が良いかな。
Using stm As New System.IO.MemoryStream()
bmp.Save(stm, Imaging.ImageFormat.Png)
Return stm.ToArray()
End Using
(Seek メソッドも不要だったので、取り除きました)
魔界の仮面弁士さん、早速ありがとうございます。
バッチリ出来ました。MemoryStream() の使い方も
ちょっとだけ理解出来ました。
ちなみに、Byte 配列から Bitmap 型にするには
こんな感じで良いのでしょうか。
これで一応動いていますが大きいサイズの画像だと
遅くなるかなと思うので、違う方法があると思うのですが・・。
Dim bData As Byte() ' DBに保存した画像データが入っている
PictureBox1.Image = SetBinary(bData)
Public Shared Function SetBinary(ByVal bData As Byte()) As Bitmap
Dim iCnt As Integer
Using stm As New System.IO.MemoryStream()
For iCnt = 0 To bData.Length - 1
stm.WriteByte(bData(iCnt))
Next iCnt
Return New Bitmap(stm, True)
End Using
End Function
Using stm As New System.IO.MemoryStream(bData)
Return New Bitmap(stm, True)
End Using
魔界の仮面弁士 さん、助かりました。
今後とも宜しくお願いします。
ツイート | ![]() |