Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to retrieve images of greater than 1 mb in size from sql server in windpws application using c#.
When I try to insert the image of greater than 1 mb in size, it is inserted, but when I try to retrieve the image the runtime error is occurring i.e. "Parameter is not valid".
The code I tried to retrieve the image.


How can I retrieve the image of bigger size i.e. greater than 1 mb in size.

Thanks in Advance ...!!!

What I have tried:

C#
name = cbCust_Name.SelectedValue.ToString();  
//btnDisplay_Click(sender, e);  
SqlConnection cnn = new SqlConnection(conString);  
MemoryStream stream = new MemoryStream();  
cnn.Open();  
SqlCommand command = new SqlCommand("Select Cust_Image from Photo where Cust_Name= '" + name + "'", cnn);  
byte[] image = (byte[])command.ExecuteScalar();  
stream.Write(image, 0, image.Length);  
//cnn.Close();  
Bitmap bitmap = new Bitmap(stream);  
pictureBox1.Image = bitmap;
Posted
Updated 23-Feb-16 19:34pm
v2

1 solution

The problem is that new Bitmap(ms) is going to read the data from the stream's current position. And if the stream is currently positioned at the end of the data, it's not going to be able to read anything, hence the problem occurs. Try with below code:
C#
string name = cbCust_Name.SelectedValue.ToString();  

SqlConnection cnn = new SqlConnection(conString);
cnn.Open();  
SqlCommand command = new SqlCommand("Select Cust_Image from Photo where Cust_Name= '" + name + "'", cnn);  
byte[] image = (byte[])command.ExecuteScalar();
cnn.Close();  

MemoryStream ms = new MemoryStream(image);
ms.Seek(0, SeekOrigin.Begin);
Bitmap bitmap = new Bitmap(ms);
pictureBox1.Image = bitmap;

Suggestions: Please try to use Parameterised query when you are using inline SQL query. It helps to avoid SQL injection. Secondly close the connection and memory stream object properly.
 
Share this answer
 
v2

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