Picture to ByteArray Closed Stream c# - c#

I looked around everywhere and we stucked (Since 2 days we are googleing, so pls dont "lmgty"). We want to Convert an image to byte[], in c#.NET, found this method:
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
It says:
Thrown: "Cannot access a closed Stream." (System.ObjectDisposedException) Exception Message = "Cannot access a closed Stream.", Exception Type = "System.ObjectDisposedException"
For fixing it i tryed:
ms.Flush();
Our goal is to save the byte[] into the database. We also tried this:
var parameter = new SqlParameter("#Value", SqlDbType.Image)
{
Value = Image
};
cmd.Parameters.Add(parameter);
It says that Bitmap to Byte[] has failed.
The whole insterting command is:
private void CreateEntry()
{
var conn = new SqlConnection(ConfigurationManager.ConnectionStrings[Connectionstring].ConnectionString);
var cmd = new SqlCommand("INSERT INTO _FH_Picture ID, Value, BindItem, Owner, McItemRelated, UploadTime VALUES #ID, #Value, #BindItem, #Owner, #McItemRelated, #UploadTime", conn) { CommandType = CommandType.Text };
cmd.Parameters.AddWithValue("#ID", ID.ToString());
cmd.Parameters.AddWithValue("#Value", imageToByteArray(value));
cmd.Parameters.AddWithValue("#Owner", _owner);
cmd.Parameters.AddWithValue("#BindItem", _bindtitem);
cmd.Parameters.AddWithValue("#McItemRelated", _mcitemrelated);
cmd.Parameters.AddWithValue("#UploadTime", _uploadtime);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}

Related

Read Image stored in Oracle in Long (Finacle)

I want to read the image stored in Oracle Long datatype. Number of images are stored in a remote Oracle database in a column with datatype long. I just need to retrieve those images and show them on my aspx page. I could retrieve the image from database but when tried to caste it to byte array, it threw error that, string can not be converted to byte[]'. Anybody have any suggestions on how to retrieve these images stored in long column in the database. I have already tried various beginner techniques.
OracleDataReader TableReader = null;
string connectionString = ConfigurationManager.ConnectionStrings["System.Data.OracleClient"].ConnectionString;
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
command = new OracleCommand("Select * From IMAGE_TABLE where CUST_ID ='xxxxxxxx' fetch first 1 rows only", connection);
command.InitialLONGFetchSize = -1;
TableReader = command.ExecuteReader();
while (TableReader.Read())
{
try
{
var tt = Convert.ToInt32(TableReader[14]);
byte[] bytes = new byte[tt];
//long bytesRead = TableReader.GetBytes(13, 0, bytes, 0, tt);
//FileStream file = new FileStream("d:\\Img1.jpg", FileMode.Create, FileAccess.Write);
//file.Write(bytes, 0, (int)bytesRead);
//file.Close();
byte[] blob_ =System.Text.UTF32Encoding.ASCII.GetBytes(TableReader[13].ToString());
using (MemoryStream ms = new MemoryStream(blob_))
{
image = Image.FromStream(ms);
}
}
catch (Exception rr)
{
}
}
}

Can't read image from SQL Server using C#

When I try to read images from my SQL Server database I get an error: on the following line:
Image returnImage = Image.FromStream(ms);
An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll
Additional information: Parameter is not valid.
My code:
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
private ArrayList GetImagesFromDB(string code)
{
ArrayList images = new ArrayList();
SqlCommand selectImagesCommand = new SqlCommand("SELECT * FROM images WHERE code = '"+code+"'");
selectImagesCommand.Connection = myConnection;
if(myConnection.State == ConnectionState.Closed){
myConnection.Open();
}
SqlDataReader imagesReader = selectImagesCommand.ExecuteReader();
while (imagesReader.Read())
{
byte[] newimagebytesRecord = (byte[])imagesReader["image"];
images.Add(byteArrayToImage(newimagebytesRecord));
}
return images;
}
Use SqlDataReader.GetStream method.
So you would do something along the lines:
using(SqlDataReader imageReader = selectSingleImageCommand.ExecuteReader(CommandBehavior.SequentialAccess))
{
if (imageReader.Read())
{
using (Stream backendStream = imageReader.GetStream(0))
{
//Do whater you need with the Stream
}
}
}
You migh also be able to iterate over the reader (selecting multiple images) and open stream by stream - I haven't use it that way though, so I'm not 100% sure.
try
{
SqlCommand cmdSelect=new SqlCommand("select Picture" +
" from tblImgData where ID=#ID",this.sqlConnection1);
cmdSelect.Parameters.Add("#ID",SqlDbType.Int,4);
cmdSelect.Parameters["#ID"].Value=this.editID.Text;
this.sqlConnection1.Open();
byte[] barrImg=(byte[])cmdSelect.ExecuteScalar();
string strfn=Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs=new FileStream(strfn,
FileMode.CreateNew, FileAccess.Write);
fs.Write(barrImg,0,barrImg.Length);
fs.Flush();
fs.Close();
pictureBox1.Image=Image.FromFile(strfn);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.sqlConnection1.Close();
}

C# Error converting bytearray from database to image

I have run into a problem with a C# project I'm working at. I get an object array containing all the selected items from a database row with the function:
public static object[] getResultSet (string statement)
{
object[] resultArray=null;
OleDbConnection con = getConnection();
OleDbCommand command = new OleDbCommand(statement, con);
try
{
con.Open();
OleDbDataReader reader = command.ExecuteReader();
if (reader.Read())
{
resultArray = new object[reader.FieldCount];
reader.GetValues(resultArray);
}
reader.Close();
}
catch (OleDbException ex)
{
MessageBox.Show(ex.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
con.Close();
}
return resultArray;
}
and I have this object array in which I have stored all the results:
object[] obj = getResultSet("SELECT * FROM Rooms WHERE ID=1");
I certainly know that the object obj[2] is a BLOB in my database because the debugger shows over 3mil bytes for my object. But when I try to convert the bytes array from the object I get informed that I didn't handle an exception:
"An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll
Additional information: Parameter is not valid."
Here's the function I'm using to convert the bytes array to an image:
public static Image imageFromByteArray(byte[] bytearray)
{
Image img;
ImageConverter imgCon = new ImageConverter();
img=(Image)imgCon.ConvertFrom(bytearray);
return img;
}
I tried to call
imageFromByteArray((byte[])obj[2]);
but it didn't work, even with another function that converts an object to a byte array:
public static byte[] objectToByteArray(object obj)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
I have found the solution. Before, I had manually added the image to the database, this is why it behaved so strangely.
In order to be recognized by my program, all the images needed to be programatically added.

How to save an image into a database?

I am using WPF to insert an image to my MySQL database. I can upload the file to image control but I don't know how to save it.
Here is what I've done so far. The emp_image is the image control that displays the photo.
private void btn_save_image_click(object sender,...)
{
Mysqlconnection cn= new mysqlconnection(connectionstring);
byte[] imagedata;
imagedata=File.ReadAllBytes(emp_img); //..here is error,it says has invalid arguments..//
mysqlcommand= new mysqlcommand("insert into dep_table(photo)values(?data)",cn);
cmd.parameters.addwithvalue("?data", imagedata);
cn.open();
cmd.executeNonQuery();
cn.close();
}
you need to convert the image source to byte[] :
public static byte[] ImageToArray(BitmapSource image)
{
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
using (var stream = new MemoryStream())
{
encoder.Save(stream);
return stream.ToArray();
}
}
Then, call the function:
imagedata = ImageToArray((BitmapSource)emp_img.Source);
Seems you are not passing the file path.
Please check File::ReadAllBytes Method
Should be something like
var imagedata=File.ReadAllBytes(filepath);

System.ArgumentException: Parameter is not valid [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
“Parameter not valid” exception loading System.Drawing.Image
I am inserting a image in the DB.
Here's my code
public class ImageUtils
{
const int sizeThumb = 69;
public static int uploadImage(int memberid, Image thumbimage)
{
MemoryStream stream = new MemoryStream();
thumbimage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
Byte[] thumbbytes = stream.ToArray();
//int length = Convert.ToInt32(data.Length);
//byte[] thumbimage = new byte[length];
//data.Read(thumbimage, 0, length);
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["FMMImages"].ConnectionString);
SqlCommand command = new SqlCommand("update Images_temp set thumbimage = #thumbimage where memberid=#memberid", connection);
SqlParameter param0 = new SqlParameter("#thumbimage", SqlDbType.Image);
param0.Value = thumbbytes;
command.Parameters.Add(param0);
connection.Open();
object result = command.ExecuteScalar();
connection.Close();
if (result != null)
{
return System.Convert.ToInt32(result);
}
else
{
return 0;
}
}
aspx.cs where I'm calling the uploadimage
image CroppedWaterMarkImage
......
ImageUtils.uploadImage(memberid, CroppedWaterMarkImage);
Error in the uploadimage function:
MemoryStream stream = new MemoryStream();
thumbimage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
Byte[] thumbbytes = stream.ToArray();
System.ArgumentException: Parameter is not valid.
Thanks
Sun
These guys encountered a similar problem with Images and MemoryStream() because of a memory leak:
http://blog.lavablast.com/post/2007/11/29/The-Mysterious-Parameter-Is-Not-Valid-Exception.aspx
This link was resolved by calling System.Drawing.Bitmap instead of System.Drawing.Image:
http://forums.asp.net/t/1705636.aspx/1
http://msdn.microsoft.com/en-us/library/ms524632%28v=vs.90%29.aspx
Either (memory leak/corruption and/or choice of API) could be applicable to your scenario.
Also make sure the image file is valid.

Categories

Resources