Solved

asp image to byte[]

Posted on 2011-09-30
8
266 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

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
Technology Partners: 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

724 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