Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have Image and I have convert into the byte... same as Image visible in notpad...that i want.......

C#
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
 MemoryStream ms = new MemoryStream();
 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 return  ms.ToArray();
}

that i know but how to pass image in above code,can't understand that code,,can anyone help me please....
Posted
Updated 25-Oct-13 3:18am
v2
Comments
joshrduncan2012 25-Oct-13 9:17am    
Ok, so what's your question?
Nandakishore G N 25-Oct-13 9:26am    
System.Drawing.Image useruploadedimage = System.Drawing.Image.FromStream(FileUpload.PostedFile.InputStream);
will get you the image pass as parameter and try

Once i wanted functionality similler to your problem and i have done something like this.
This is not exact solution to your problem but it may give you an idea.
C#
private void btnBrowse_Click(object sender, EventArgs e)
{

        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Title = "choose your file";

        if (ofd.ShowDialog() == DialogResult.OK)
        {
           filepath = ofd.FileName.ToString();
        }
        long size = (new FileInfo(filepath).Length);
        FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.ReadWrite);
        byte[] data = new byte[size];

        try
        {
           fs.Read(data, 0, (int)size);
           //This read stream from FileStream fs and write to byte array data.
           //Here you Get the data[] byte array. now you can use it as per your requirement
        }
        catch (Exception ex)
        {
            MessageBox.Show("error in file read..." + ex.ToString());
        }
        finally
        {
            fs.Close();
            fs.Dispose();
        }

        MemoryStream ms = new MemoryStream();
        ms.Write(data, 0, (int)size);
        //Here you can take new file and add this memory stream to it.

        destPath = Application.StartupPath.ToString() + "\\images";
        destPath = Path.Combine(destPath, Path.GetFileName(filepath));
        File.Copy(filepath, destPath);

}


Hope This help
 
Share this answer
 
v3
Comments
Ankit Gajera 26-Oct-13 1:19am    
does not get the size....................
Pratik Bhuva 28-Oct-13 1:34am    
if (ofd.ShowDialog() == DialogResult.OK)
{
filepath = ofd.FileName.ToString();
}
long size = (new FileInfo(filepath).Length);

check whether this if condition is executed or not???
and check what is set to your filepath variable.
If your condition is not executed then there is a problem with file uploading
Try this to convert image to byte[]:

C#
public byte[] ConvertImageToByteArray(Image imageIn)
        {
            var memoryStream = new MemoryStream();
            imageIn.Save(memoryStream, ImageFormat.Jpeg);
            memoryStream.Close();
            return memoryStream.ToArray();
        }


And this to convert it back to Image if you need:

C#
public Image ConvertByteArrayToImage(byte[] byteArrayIn)
        {
            var memoryStream = new MemoryStream(byteArrayIn);
            var returnImage = Image.FromStream(memoryStream);
            memoryStream.Close();
            return returnImage;
        }


Hope it helps :)
 
Share this answer
 
Comments
Ankit Gajera 25-Oct-13 9:23am    
i know that but how to pass imagename into the imagein...does not convert it...
Marvin Ma 25-Oct-13 9:26am    
Where do you get the Image from?
From your hard disk?
Ankit Gajera 25-Oct-13 9:29am    
get image from my Application.startup page....like Application.StartUp.
Marvin Ma 25-Oct-13 9:31am    
Ok, if it is really an Image you just can invoke your method and pass this image...
imageToByteArray(yourImage)
Ankit Gajera 25-Oct-13 9:33am    
get already image name in textbox ...and that i want to pass in the imagein.
btn_Click event ()
{
??? imaagein = txtimg.text;
ConvertImageToByteArray(imageIn);
}

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