Bind Image Data to PicBox

G

Guest

Hi,
I'm just getting started w/ VB.NET. I've been doing database development in
MS Access, both as Access databases or as a front end for SQL Server. I need
to create a VB.NET app to do some specific functions with SQL Server data.
The main problem I'm having is trying to bind data from a SQL Server image
data type field to a PictureBox control. For example, the image data field
in my table will hold a JPG file of a scanned document (file size probably no
greater than 300KB). I just want to be able to display the JPG in a
PictureBox control after a user enters some kind of parameter to return a
specific record. I just can't seem to get it right. I left a message in a
SQL Server forum and was told to try here.
Any help would be greatly appreciated!!!
 
K

Ken Tucker [MVP]

Hi,

Images are stored in a database as an array of byte. To convert an
array of byte to a bitmap you read the byte array in to a memory stream and
create a bitmap from it. I created an Video Capture Box which adds a
ByteImage property to a picture box which you can bind to. You can download
it from gotdotnet
http://www.gotdotnet.com/Community/...mpleGuid=daf38a9e-0e33-4777-93d3-c664ae2f91f8

VideoCapture1.DataBindings.Add("ByteImage", dt, "Picture")

Ken
---------------
Hi,
I'm just getting started w/ VB.NET. I've been doing database development in
MS Access, both as Access databases or as a front end for SQL Server. I
need
to create a VB.NET app to do some specific functions with SQL Server data.
The main problem I'm having is trying to bind data from a SQL Server image
data type field to a PictureBox control. For example, the image data field
in my table will hold a JPG file of a scanned document (file size probably
no
greater than 300KB). I just want to be able to display the JPG in a
PictureBox control after a user enters some kind of parameter to return a
specific record. I just can't seem to get it right. I left a message in a
SQL Server forum and was told to try here.
Any help would be greatly appreciated!!!
 
C

Cor Ligthert

Paul,

You use the word Bind image. What means in terms of VBNet that you have a
kind of datasource that is a collection or a datatable.

From your text however I get an other idea. Can you tell us more what you
mean where comes the picture from and how do you get it..

Cor
 
G

Guest

Cor:

The image data comes from an image data type field in a SQL Server database.
The form I built has 2 or 3 text boxes that display data from other fields
in the table, and I have a PictureBox control to (hopefully) display the
image. (Note: What I'm trying to do is almost the same thing as a Northwind
database sample where an Employee's picture is displayed on a Windows Form
along with other data like Name, etc.) I've tried code samples and put them
in as exact as I can to the examples, but I still get an "Invalid Parameter"
error in "System.Drawing.dll".

Here's the code I'm using, including some commented-out attempts that didn't
work either:

Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Imaging

Public Class Form1
Inherits System.Windows.Forms.Form
'(Windows form designer generated code here...)
Private Sub cmdLoadData_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdLoadData.Click
Me.DsProjCOs1.Clear()
Me.OleDbDataAdapter1.Fill(DsProjCOs1)
End Sub

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNext.Click
'Me.BindingContext(Me.DsProjCOs1, "tblChangeOrder").Position += 1
Dim c As Integer = Me.DsProjCOs1.Tables("tblChangeOrder").Rows.Count
If c > 0 Then
If Not Me.DsProjCOs1.tblChangeOrder _
(Me.BindingContext(Me.DsProjCOs1,
"tblChangeOrder").Position)("co_apprdoc") _
Is DBNull.Value Then
Dim bytBLOBData() As Byte = _

Me.DsProjCOs1.Tables("tblChangeOrder").Rows(Me.BindingContext(Me.DsProjCOs1, _
"tblChangeOrder").Position)("co_apprdoc")
Dim stmBLOBData As New System.IO.MemoryStream(bytBLOBData)
Me.picCOAppr.Image = Image.FromStream(stmBLOBData)

'********************************************************************
'*** Commented lines below were another attempt that didn't
work. ***
'Dim bytBLOBData() As Byte = _
' DsProjCOs1.Tables("tblChangeOrder").Rows(c -
1)("co_apprdoc")
'Dim bmp As New Bitmap(stmBLOBData)
'Me.picCOAppr.Image = bmp

'********************************************************************
End If
End If
End Sub

(End of code sample.)

It's possible that I'm making some kind of glaring error, but I haven't
found it. I'm really new to VB.NET. Also, just as a note, I don't have any
trouble generating a dataset for this form. The non-image data comes up just
fine.

Thanks for your help! If I can clarify further, let me know.
 
G

Guest

Ken:

I downloaded your sample and am going to give it a try. Thank you very much
for your input. I hope I can get things working. I will reply again either
way.

Regards,
PaulJS
 
C

Cor Ligthert

Paul,

First of all do I when I have this kind of problems create some extra steps
in my code.
Now it is nice connected however hard to debug and even more difficult to
show to somebody else.

Secondly, I don't see the reason for the binding in this. You use a "next
button".

Here a piece from a program from me, not a sample, so I cannot show it
complete because that will be to much.
\\\
Private Sub btnNext_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnNext.Click
If number < dsFototabel.Tables(0).Rows.Count - 1 Then
number += 1
setPicture()
End If
End Sub
///

And then there is in fact nothing more than

\\\
arrPicture = CType(dsFototabel.Tables(0).Rows(number)("thumb"), Byte())
ms = New MemoryStream(arrPicture)
picCurrent.Image = Image.FromStream(ms)
///

However it can as well be that you are using OLE pictures, than you have to
set (hopefully because there are more formats in that)

\\\
Dim ms1 As New System.IO.MemoryStream(arrPicture, 78, arrPicture.Length -
78)
///

I hope this helps a little bit?

Cor
 
G

Guest

Hi Cor,

Yes, thank you, it really did!

I´ve been brooding on this problem for some time now.
Thanks to your hints I came up with the following code in C#,
which works OK for me. But I´m actually not using Binding ...

I have a small MS Access DB called BMprov.mdb for trial.
It contains only one table called PicTest, and this table
contains only three columns:
PicNo (numeric runing number)
Name (character string)
PicDraw (bitmap picture, an ActiveX-object derived from a jpg-file in the MS
Access environment).

Here is the essential code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;

.....

public void runPicture(string picNo)
{
// ICa 2005-06-03 20:38
Cursor tmp = this.Cursor;
this.Cursor = Cursors.WaitCursor;

try
{
OleDbConnection picC = new OleDbConnection();
picC.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;" + @"data
source = C:\Waterials\Databas\BMprov.mdb";


picC.Open();

string sqlString = "SELECT * FROM PicTest WHERE PicNo = " + picNo;
OleDbDataAdapter picA = new OleDbDataAdapter (sqlString, picC);
OleDbCommandBuilder picB = new OleDbCommandBuilder(picA);
DataSet picD = new DataSet();
picA.Fill(picD, "PicTest");
DataTable picT = picD.Tables["PicTest"];

txPicNo.Text = picT.Rows[0]["PicNo"].ToString();
txName.Text = picT.Rows[0]["Name"].ToString();

Byte[] draw = (Byte[]) picT.Rows[0]["PicDraw"];
MemoryStream ms = new MemoryStream(draw, 78, draw.Length -78);
pbDrawing.Image = Image.FromStream(ms);
ms.Close();
}
catch (Exception ex)
{
MessageBox.Show("Fel vid framtagning av bild: " + ex.Message,
"Bild-fel", MessageBoxButtons.OK,

MessageBoxIcon.Error);
}
this.Cursor = tmp;
}

It sure took me some good time to figure all this out ...

Best regards
/Jingo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top