?
Solved

asp image to byte[]

Posted on 2011-09-30
8
Medium Priority
?
267 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 1000 total points
ID: 36891206
Also you can use:
byte[] myImageBytes = System.IO.File.ReadAllBytes("MyImageFilePath");

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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