By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,187 Members | 1,638 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,187 IT Pros & Developers. It's quick & easy.

Display image from sql server using c#

P: n/a
I already stored the image in the SQL database. How do I retrieve it? Any
help is appreciated.
Nov 17 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Bama,

1) Retrieve the image from the database as a byte[].
2) Feed the byte[] to a MemoryStream
3) Create an Image using Image.FromStream
On Fri, 17 Nov 2006 07:20:01 +0100, Bama <rb***@hotmail.comwrote:
I already stored the image in the SQL database. How do I retrieve it? Any
help is appreciated.


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 17 '06 #2

P: n/a
Thank you for your reply. I managed to display the image. Now I want to
restrict the image size and also the type. How do I do that?

It is an employee profile. I store the images for each of them. How do I
display a blank screen if dont have image for particular person.

Thanks...
--
-----r b a m a------
"Morten Wennevik" wrote:
Hi Bama,

1) Retrieve the image from the database as a byte[].
2) Feed the byte[] to a MemoryStream
3) Create an Image using Image.FromStream
On Fri, 17 Nov 2006 07:20:01 +0100, Bama <rb***@hotmail.comwrote:
I already stored the image in the SQL database. How do I retrieve it? Any
help is appreciated.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 17 '06 #3

P: n/a
You can create a new Bitmap with the proper size and draw the original
image to the new Bitmap. The code below will compress (or expand) the
original image to 100x130 pixels

Image i = Image.FromFile(filename);

Bitmap bmp = new Bitmap(100, 130);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(i, 0, 0, 100, 130);
}

For blank images you need to check if there are no bytes in the image
field, or maybe check for DBNull.Value if the field is empty (null).

Instead of creating a new bitmap in the code above you can use a default
image for indicating no profile image is available, and, something like
this:

Bitmap bmp = (Bitmap)Bitmap.FromFile(defaultimage);

Image i = GetImageFromDatabase(employeeId);

if(i != null)
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(i, 0, 0, 100, 130);
}
}

On Fri, 17 Nov 2006 09:54:02 +0100, Bama <rb***@hotmail.comwrote:
Thank you for your reply. I managed to display the image. Now I want to
restrict the image size and also the type. How do I do that?

It is an employee profile. I store the images for each of them. How doI
display a blank screen if dont have image for particular person.

Thanks...



--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.