Solved

asp image to byte[]

Posted on 2011-09-30
8
263 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASCX file or a newer alternative? 1 35
Help with query 3 25
Sort GridView by ID Descending 1 13
Adding Row and Cell on a table 14 20
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.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

777 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