I have 2 methods, shown below, and i can't get it to work. I am trying to open a .png
file from a OpenFileDialog
and display it on a ImageBox
:
public static Bitmap ToBitmap(this string input)
{
List<byte> splitBytes = new List<byte>();
string byteString = "";
foreach (char i in input)
{
byteString += i;
if (byteString.Length == 3)
{
splitBytes.Add(Convert.ToByte(byteString));
byteString = "";
}
}
if (byteString != "")
splitBytes.AddRange(Encoding.ASCII.GetBytes(byteString));
using (var ms = new MemoryStream(splitBytes.ToArray()))
{
var img = Image.FromStream(ms);
Bitmap output = new Bitmap(img);
return output;
}
}
public static string StringFromFile(string input)
{
StreamReader sr = new StreamReader(input);
string file = string.Empty;
while (sr.EndOfStream == false)
{
file += sr.Read();
}
return file;
}
In another file i tried to use the method:
OpenFileDialog OFD = new OpenFileDialog();
OFD.Filter = "Images (*.png)|*.png";
OFD.ShowDialog();
pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
pictureBox1.BackgroundImage = StringToBitmapConverter.ToBitmap(StringToBitmapConverter.StringFromFile(OFD.FileName));
But I get this exception:
System.OverflowException: 'Value was either too large or too small for an unsigned byte.'
Please help!
I am using these methods in a class called StringToBitmapConverter
, and there is an error that is giving me trouble, can anyone help me?