Solved

asp image to byte[]

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

Author Comment

by:JCWEBHOST
ID: 36891207
string filename

how do i get the correct path?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now