Bitmap to Bytes and Back

  • Thread starter Thread starter Ron M. Newman
  • Start date Start date
R

Ron M. Newman

Hi,

I'm interested in the process of taking a live bitmap oject with an image
already loaded in it, and representing it as a string such that a whole
bitmap carrying the same image can be constructed using only that string. It
doesn't HAVE to be a string, it could be an array of bytes or whatever, as
long as I can write code that converts it to a string carrying hex code
someting like "FF00FF" etc.

I know we're not in C++, but ... Any pointers ;-)

Thanks
-Ron
 
Ron M. Newman said:
I'm interested in the process of taking a live bitmap oject with an image
already loaded in it, and representing it as a string such that a whole
bitmap carrying the same image can be constructed using only that string. It
doesn't HAVE to be a string, it could be an array of bytes or whatever, as
long as I can write code that converts it to a string carrying hex code
someting like "FF00FF" etc.

---8<---
using System;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Windows.Forms;

class App
{
static void Main(string[] args)
{
MemoryStream temp = new MemoryStream();
using (Image image = Image.FromFile(args[0]))
image.Save(temp, ImageFormat.Jpeg);
temp.Position = 0;

string asString = Convert.ToBase64String(temp.ToArray());
Console.WriteLine(asString);

MemoryStream loaded = new
MemoryStream(Convert.FromBase64String(asString));
using (Image image = Image.FromStream(loaded))
{
Form form = new Form();
form.Paint += delegate(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(image, Point.Empty);
};
form.ClientSize = image.Size;
Application.Run(form);
}
}
}
--->8---

-- Barry
 
wow, cool. thanks!

Barry Kelly said:
Ron M. Newman said:
I'm interested in the process of taking a live bitmap oject with an image
already loaded in it, and representing it as a string such that a whole
bitmap carrying the same image can be constructed using only that string.
It
doesn't HAVE to be a string, it could be an array of bytes or whatever,
as
long as I can write code that converts it to a string carrying hex code
someting like "FF00FF" etc.

---8<---
using System;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Windows.Forms;

class App
{
static void Main(string[] args)
{
MemoryStream temp = new MemoryStream();
using (Image image = Image.FromFile(args[0]))
image.Save(temp, ImageFormat.Jpeg);
temp.Position = 0;

string asString = Convert.ToBase64String(temp.ToArray());
Console.WriteLine(asString);

MemoryStream loaded = new
MemoryStream(Convert.FromBase64String(asString));
using (Image image = Image.FromStream(loaded))
{
Form form = new Form();
form.Paint += delegate(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(image, Point.Empty);
};
form.ClientSize = image.Size;
Application.Run(form);
}
}
}
--->8---

-- Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top