我想把一副绘制好的画面存放到GraphicsStream中 以下代码写在一个按钮事件中 点击按钮绘制并储存图片到GraphicsStream
Bitmap bp = new Bitmap(panel1.ClientRectangle.Width, panel1.ClientRectangle.Height);
bp.MakeTransparent(Color.FromArgb(0, Color.White));
Graphics g = Graphics.FromImage(bp);
Graphics g1 = panel1.CreateGraphics();
g1.Clear(Color.White);
g.DrawString("测试字符串", new Font("宋体", 10), new SolidBrush(Color.Black), new Point(100, 100));
g1.DrawImage(bp, new Point(0, 0));
//以上就是绘制图片到 panel1中
GraphicsStream gs = new GraphicsStream(new IntPtr(IntPtr.Size), IntPtr.Size, true, true);bp.Dispose();
bp.Save(gs, ImageFormat.Bmp); //此处异常:参数无效 如果我改用MemoryStream定义gs一样会出这个异常
g.Dispose();
g1.Dispose();
Bitmap bp = new Bitmap(panel1.ClientRectangle.Width, panel1.ClientRectangle.Height);
bp.MakeTransparent(Color.FromArgb(0, Color.White));
Graphics g = Graphics.FromImage(bp);
Graphics g1 = panel1.CreateGraphics();
g1.Clear(Color.White);
g.DrawString("测试字符串", new Font("宋体", 10), new SolidBrush(Color.Black), new Point(100, 100));
g1.DrawImage(bp, new Point(0, 0));
//以上就是绘制图片到 panel1中
GraphicsStream gs = new GraphicsStream(new IntPtr(IntPtr.Size), IntPtr.Size, true, true);bp.Dispose();
bp.Save(gs, ImageFormat.Bmp); //此处异常:参数无效 如果我改用MemoryStream定义gs一样会出这个异常
g.Dispose();
g1.Dispose();
看看你使用的Bitmap类中Save方法的参数说明
我是在winform窗体中 一个按钮事件里写的
----------------------------
我用MemoryStream没有问题的,不过你可以这样改一下看看:
bp.Save(gs, bp.RawFormat); 或者也可能就是这个Bitmap本身有问题。
是不是在Mobile中不支持?
bp.MakeTransparent(Color.FromArgb(0, Color.White));
Graphics g = Graphics.FromImage(bp);
Graphics g1 = panel1.CreateGraphics();
g1.Clear(Color.White);
g.DrawString("测试字符串", new Font("宋体", 10), new SolidBrush(Color.Black), new Point(100, 100));
g1.DrawImage(bp, new Point(0, 0));
//以上就是绘制图片到 panel1中
MemoryStream gs = new MemoryStream(); // 这个地方改成MemoryStream 还是参数无效bp.Dispose();
bp.Save(gs, ImageFormat.Bmp); //此处异常:参数无效 如果我改用MemoryStream定义gs一样会出这个异常
g.Dispose();
g1.Dispose();麻烦谁有用MemoryStream成功的代码发一下 我在机器上试试