Click here to Skip to main content
15,886,024 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
i have write this code for extracting bitplane1 of my image . but i have exception
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;


namespace bitplane
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)

        {
          //  Image grayImage;
            OpenFileDialog o = new OpenFileDialog();
            o.ShowDialog();
   
            byte[] x = File.ReadAllBytes(o.FileName);
        
            byte maskbyte1 = 2;
            int [] newpix= new int [x.Length];
      for (int i = 0; i < x.Length; i++)
           {


               newpix[i] = x[i] & maskbyte1;

               string px=newpix[i].ToString();
     

              x[i] = Convert.ToByte(px);
              
        }
            MemoryStream ms = new MemoryStream(x);
            Image myImage = Image.FromStream(ms);

        myImage.Save(@"C:\Users\Public\Pictures\Sample Pictures\New folder\fgh.jpg");
        }
    }
}
Posted
Updated 5-Mar-14 2:04am
v3

use the below code in httphandler to convert byte array to image


C#
context.Response.ContentType = "image/jpeg";//get image content type of selected file
Stream strm = new MemoryStream(x);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
  context.Response.OutputStream.Write(buffer, 0, byteSeq);
  byteSeq = strm.Read(buffer, 0, 4096);
}
 
Share this answer
 
v2
Comments
ilia71 5-Mar-14 8:09am    
thanks but do you read my code ? i get an image and convert it to a byte array so after i change this byte array i want to convert this new byte array to image ? could you please give me some advice ?
best regards
Sergey Alexandrovich Kryukov 5-Mar-14 12:49pm    
What is really bad in your code: immediate constant 4096 taken from nowhere and corresponding limitation. In fact, there is absolutely no need in it.
—SA
You are using correct approach, but only in two lines related to MemoryStream and the fragment where you read bytes from a file. Everything else is some gibberish I cannot understand. Throw it out and you will have a working solution. If you have some problems, use the debugger.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900