Solved

asp image to byte[]

Posted on 2011-09-30
8
265 Views
Last Modified: 2012-08-13
Hey guys i have an image in my web app in the image folder and i want to convert that to byte[]  in c# code , please help?
0
Comment
Question by:JCWEBHOST
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36891194
Check this
private byte[] ReadImage(string filename)
    {
        
        try
        {
                FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
 
                BinaryReader br = new BinaryReader(fs);
 
                byte[] image = br.ReadBytes((int)fs.Length);
 
                br.Close();
 
                fs.Close();
 
                return image;
            
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Open in new window

0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36891199
0
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 250 total points
ID: 36891206
Also you can use:
byte[] myImageBytes = System.IO.File.ReadAllBytes("MyImageFilePath");

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:JCWEBHOST
ID: 36891207
string filename

how do i get the correct path?
0
 

Author Comment

by:JCWEBHOST
ID: 36891214
got an erorr:

Error: Sys.WebForms.PageRequestManagerServerErrorException: Could not find file 'C:\Documents and Settings\Administrator\Desktop\paintec.co.za\images\no image.jp
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36891218
Where image (trying to convert to byte array) is located?
OR  You are using file upload control and convert it into byte array and saving it into database?

If the image is in folder then you can get the correct path like

Server.MapPath("~/images/your_image_name");
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36891240
Check that your file exists in "C:\Documents and Settings\Administrator\Desktop\paintec.co.za\images\", have you mistype "no image.jp"? try "no image.jpg"
0
 
LVL 17

Assisted Solution

by:sonawanekiran
sonawanekiran earned 250 total points
ID: 36891296
If you are giving hardcode path
then path could be

 string filename=@"C:\Documents and Settings\Administrator\Desktop\paintec.co.za\images\your_image_name.jpg";

OR

 string filename="C:\\Documents and Settings\\Administrator\\Desktop\\paintec.co.za\\images\\your_image_name.jpg";
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question