Images in a combobox

M

mnsindhu74

I want to display images in a databound combobox. The combobox is
ownerdrawn. The images are taken from a datacolumn in a datatable. The
image comes as a System.byte[]. So how could I get the format of the
image ie jpeg, bmp, gif etc.

Can a datacolumn with a datatype of System.Byte[] contain other objects
than images?

My requirement is to correctly identify the fields that contain images
and display them as treenodes for an ImageMember property (Similar to
DisplayMember Property) which has been newly added to the combobox.

thanks
 
M

Morten Wennevik

Hi,

Image i = Image.FromStream(new MemoryStream(byte[]));

if(i.RawFormat == ImageFormat.Jpeg)
// Image is JPG
else if(i.RawFormat == ImageFormat.Bmp)
// ...

A byte array can contain anything. It is up to you to encode/decode the data.



I want to display images in a databound combobox. The combobox is
ownerdrawn. The images are taken from a datacolumn in a datatable. The
image comes as a System.byte[]. So how could I get the format of the
image ie jpeg, bmp, gif etc.

Can a datacolumn with a datatype of System.Byte[] contain other objects
than images?

My requirement is to correctly identify the fields that contain images
and display them as treenodes for an ImageMember property (Similar to
DisplayMember Property) which has been newly added to the combobox.

thanks
 
M

mnsindhu74

Thanks for the reply.

How to know whether the incoming byte[] is of an image or anything
else. Does the byte[] have any specific format?
 
M

Morten Wennevik

No, there is no way of telling what the byte[] is unless you test it. If it is likely that it is an image you can use a try/catch. If the try fails it is not an image, not supported image type or corrupt image.

If you use one image type only or only a few different, you can test the first bytes to see if it fits the jpg signature, or bmp signature etc.


Thanks for the reply.

How to know whether the incoming byte[] is of an image or anything
else. Does the byte[] have any specific format?
 
M

mnsindhu74

thanks again for helping out.

If you open the server explorer, expand the northwind database, there
is a table named categories. It has the following fields CategoryID,
CategoryName, Description, Picture.

In this Picture is of image type. if we look the properties of this
field in the Properties window It shows the Datatype as image. How to
get this info. Reflcetion on DataColumn yields the datatype as
System.byte[].
 
M

Morten Wennevik

Hm, the data type image = byte[]. However, upon closer look, the Sql image type prefixes the image with a 78 byte header so simply directly transforming the byte[] to image won't work. If you strip away the first 78 bytes you'll end up with the raw image data.

The code below will feed the image to a picturebox in the cell changed event

protected override void OnLoad(EventArgs e)
{
DataSet ds = new DataSet();
using(SqlConnection conn = new SqlConnection(@"integrated security=SSPI;
data source=<Your Database Server>; initial catalog=Northwind"))
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("SELECT Picture FROM Categories", conn);
da.Fill(ds);
}

ds.Tables[0].DefaultView.AllowNew = false;
dataGrid1.DataSource = ds.Tables[0];
dataGrid1.CurrentCellChanged += new EventHandler(dataGrid1_CurrentCellChanged);
}

private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
{
DataRow row = ((DataTable)dataGrid1.DataSource).Rows[dataGrid1.CurrentRowIndex];
byte[] data = (byte[])row[0];

MemoryStream ms = new MemoryStream();
ms.Write(data, 78, data.Length - 78);
pictureBox1.Image = Image.FromStream(ms);
}




thanks again for helping out.

If you open the server explorer, expand the northwind database, there
is a table named categories. It has the following fields CategoryID,
CategoryName, Description, Picture.

In this Picture is of image type. if we look the properties of this
field in the Properties window It shows the Datatype as image. How to
get this info. Reflcetion on DataColumn yields the datatype as
System.byte[].
 
M

Morten Wennevik

And as you wanted it in a ComboBox, here you go ...

Set DropDownStyle to DropDownList and set DrawMode to OwnerDrawFixed (or variable)

comboBox1.DataSource = ds.Tables[0];
comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem);

private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
DataRowView row = (DataRowView)comboBox1.Items[e.Index];

byte[] data = (byte[])row[3];
MemoryStream ms = new MemoryStream();
ms.Write(data, 78, data.Length - 78);

Bitmap bm = new Bitmap(ms);
e.Graphics.DrawImage(bm, e.Bounds, new Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel);
}

The pictures are 172x120 in size so you might want to adjust ItemHeight and ComboBox Width.
 
M

mnsindhu74

thank you very much.
the code snippets were very helpful .
now I can display the images in combobox correctly.
 

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