Reputation: 19
i have write this code for extracting bitplane1 of my image . but i have exceptions. actually 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 (actually i want extract bitplane1 of my image) any suggestion ?
my code is :
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");
}
}
}
my exception is for this line :
Image myImage = Image.FromStream(ms);
System.ArgumentException was unhandled Parameter is not valid.
Upvotes: 0
Views: 5135
Reputation: 188
Well, I think the first exception you may get int this code is because of this:
o.ShowDialog();
byte[] x = File.ReadAllBytes(o.FileName);
Note that doesn't matters what happenes to the open file dialog, the byte[] x = File.ReadAllBytes(o.FileName);
will always be executed once even if its value is null
. I think you should first edit your code to sthg like this:
if (o.ShowDialog() == DialogResult.OK)
{
byte[] x = File.ReadAllBytes(o.FileName);
//... and all other codes
}
Now the code will only be executed when the o
object returns OK, which means a file was selected.
The second thing is, that in your code there are a lot of places where an exception could be thrown, and in this case it's better and safer to use methods that are already exist. Here's a method that calls one:
public Image ConvertByteArrayToImage(byte[] bytes)
{
System.IO.MemoryStream stream = new System.IO.MemoryStream(bytes);
return Image.FromStream(stream);
}
This code does almost all the work that your code already contains.
But there are cases when this code throws exceptions either, so the most safe way is to put all the stuff in a try-catch block:
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 byte[] x;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (o.ShowDialog() == DialogResult.OK)
{
OpenImage(o.FileName);
SaveImage(ConvertByteArrayToImage(x), @"C:\Users\Public\Pictures\Sample Pictures\New folder\fgh.jpg");
}
}
catch
{
MessageBox.Show("error");
}
}
public void OpenImage(string path)
{
x = File.ReadAllBytes(path);
}
public void SaveImage(Image image, string path)
{
image.Save(path);
}
public Image ConvertByteArrayToImage(byte[] bytes)
{
System.IO.MemoryStream stream = new System.IO.MemoryStream(bytes);
return Image.FromStream(stream);
}
}
I inserted some methods to your code, so you can call them from anywhere, and don't have to do it inside only one event, but it does the same thing.
Hopefully helps a bit! :)
Upvotes: 1