Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

I have a windows application in c# 2008.I want to retrieve a image from sqlserver2005 and set it to the picturebox in a form.

I write the code as::

Byte[] MyData = new Byte[4028];
MyData = (Byte[])dr["PHOTO"];
MemoryStream ms = new MemoryStream(MyData);
Frm.pictureBox1.Image = Image.FromStream(ms);
ms.Close();
Posted
Updated 13-Jul-11 19:59pm
v2
Comments
Prerak Patel 13-Jul-11 5:41am    
and what is the problem?

i hope this help :-


C#
MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0]["stdPhoto"]);
                    Image imgg = Image.FromStream(ms);
                    picStdPhoto.Image = imgg;
 
Share this answer
 
Comments
raj_raje 14-Jul-11 13:11pm    
a short code for new user my 5 for you
I can see no problem. It would be great if you elaborate more. Tell us what problem you have, or what error you get.
Byte[] byteBLOBData =  new Byte[0];
byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pictureBox1.Image= Image.FromStream(stmBLOBData);


Ref:http://support.microsoft.com/kb/317701[^]
 
Share this answer
 
I tried it, it worked:
using (SqlConnection con = new SqlConnection(@"Data Source=GRIFFPC\SQLEXPRESS;Initial Catalog=myDatabase;Integrated Security=True"))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT image FROM myTable WHERE id=2", con))
        {
        SqlDataReader dr = com.ExecuteReader();
        dr.Read();
        byte[] MyData = new Byte[4028];
        MyData = (byte[]) dr["image"];
        MemoryStream ms = new MemoryStream(MyData);
        myPictureBox.Image = Image.FromStream(ms);
        ms.Close();
        }
    }
What is the problem?
 
Share this answer
 
Comments
Member 7925220 13-Jul-11 6:08am    
it gives "invalid argument" in Fmage.FromStream()........
i also use the following code to save the image from picturebox to sqlserver2005 as follows:

MemoryStream stream = new MemoryStream();
pictureBox1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
Byte[] pic = stream.ToArray();
OriginalGriff 13-Jul-11 6:19am    
Then exactly how do you save it to the DB? The code you have shown looks right, but the exception indicates there is a problem with the image data.
A silly thought: you do know that varbinary only has a maximum of 8000 bytes? The image you saved was smaller than that, wasn't it? Did you check?
Hi,

Try this example:
public static string aConnectionString = ConfigurationManager.AppSettings["AGMDat"].ToString();

public static Stream GetPhoto(int photoid, PhotoSize size)
        {
            object result;
            SqlConnection aConnection = new SqlConnection(aConnectionString);
            using (SqlCommand command = new SqlCommand("GetPhoto", aConnection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@PhotoID", photoid));
                command.Parameters.Add(new SqlParameter("@Size", (int)size));
                try
                {
                    aConnection.Open();
                    result = command.ExecuteScalar();
                }
                catch (Exception)
                {
                    return null;
                }
                finally
                {
                    command.Dispose();
                    aConnection.Close();
                }
                return new MemoryStream((byte[])result);
            }
        }


In your Store procedure (Example):

   USE [AGMDAT]
GO
/****** Object:  StoredProcedure [dbo].[GetPhoto]    Script Date: 07/13/2011 17:44:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetPhoto]
	@PhotoID int,
	@Size int
AS
	IF @Size = 1
		SELECT TOP 1 [BytesThumb] FROM [Photos] LEFT JOIN [Category] ON [Category].[CategoryID] = [Photos].[CategoryID] WHERE [PhotoID] = @PhotoID AND ([Category].[IsPublic] = @IsPublic OR [Category].[IsPublic] = 1)
	ELSE IF @Size = 2
		SELECT TOP 1 [BytesPoster] FROM [Photos] LEFT JOIN [Category] ON [Category].[CategoryID] = [Photos].[CategoryID] WHERE [PhotoID] = @PhotoID AND ([Category].[IsPublic] = @IsPublic OR [Category].[IsPublic] = 1)
	ELSE IF @Size = 3
		SELECT TOP 1 [BytesFull] FROM [Photos] LEFT JOIN [Category] ON [Category].[CategoryID] = [Photos].[CategoryID] WHERE [PhotoID] = @PhotoID AND ([Category].[IsPublic] = @IsPublic OR [Category].[IsPublic] = 1)
	ELSE IF @Size = 4
		SELECT TOP 1 [BytesOriginal] FROM [Photos] LEFT JOIN [Category] ON [Category].[CategoryID] = [Photos].[CategoryID] WHERE [PhotoID] = @PhotoID AND ([Category].[IsPublic] = @IsPublic OR [Category].[IsPublic] = 1)
	ELSE
		SELECT TOP 1 [BytesPoster] FROM [Photos] LEFT JOIN [Category] ON [Category].[CategoryID] = [Photos].[CategoryID] WHERE [PhotoID] = @PhotoID AND ([Category].[IsPublic] = @IsPublic OR [Category].[IsPublic] = 1)
RETURN



Hope this could help...

Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Regards,

Algem
 
Share this answer
 
v3
Comments
Member 7925220 13-Jul-11 6:09am    
it gives "invalid argument" in Fmage.FromStream()........ i also use the following code to save the image from picturebox to sqlserver2005 as follows: MemoryStream stream = new MemoryStream(); pictureBox1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); Byte[] pic = stream.ToArray();
Hi,


My sample application is a three tier layered application:
On above previous example is in my Data Access Layer:

this is a part of my code in my Logic Layer:
namespace mwic.facade
{
 public class PhotoManager
    {
        public Stream GetPhoto(int photoId, PhotoSize size)
        {
            MemoryStream mStream;
            mStream = (MemoryStream)mwic.da.PhotoManager.GetPhoto(photoId, size);
            return mStream;          
        }
}


The flow is like this:
Get Data = UI -> Facade ->Data Access
Return Data = Data Access -> Facade ->UI

I think you lack this code in my Facade to return to UI

Also note that I store Record image in four Sizes:

Define As:
   using System;
namespace mwic.bo
{
    [Serializable()]
    public enum PhotoSize
    {
        Small = 1,		// 100px
        Medium = 2,		// 198px
        Large = 3,		// 600px
        Original = 4	// Original Size
    }
}


I use to save images into four fields in a record in my data base
I asure that this is working cause this application is used by
one of my client.


Regards,

Algem
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900