关于MemoryStream
如下面的程序,就是截图,然后保存到流,但是不论我截的是什么图,ms里面的值都一样,请高人指点,我实在不知道哪里错了
- C# code
MemoryStream ms = new MemoryStream(); Rectangle SelectionRectangle = new Rectangle(0, 0, 500, 500); using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width,SelectionRectangle.Height)) { using (Graphics g = Graphics.FromImage(bitmap)) { g.CopyFromScreen(new Point(0,0), new Point(0,0), SelectionRectangle.Size); } bitmap.Save(ms, ImageFormat.Bmp); bitmap.Dispose(); }[解决办法]
using (Bitmap image = new Bitmap(10,10)) {
using (Graphics g = Graphics.FromImage(image)) {
g.Clear(Color.Red);
}
using (MemoryStream m = new MemoryStream()) {
image.Save(m, ImageFormat.Png); imageData = m.ToArray();
}
}