panel1.DrawToBitmap 截取panel 下半部分图片的问题
如题:这是取得整个Panel1的内容 :
Bitmap memoryImageSamSung;
panel1.DrawToBitmap(memoryImageSamSung, new Rectangle(0,0, panel1.Width, panel1.Height));
我用:
panel1.DrawToBitmap(memoryImageSamSung, new Rectangle(0,0, panel1.Width, panel1.Height/2));
总得得到的是panel1 上面的那一部分内容!!!!!
请问,如何截取panel 下半部分内容呢? --------------------编程问答-------------------- panel1.DrawToBitmap(memoryImageSamSung, new Rectangle(0,panel1.Height/2, panel1.Width, panel1.Height/2));
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/ --------------------编程问答-------------------- 请楼上试试,根本就不行,总是上部分内容!! --------------------编程问答--------------------
以下代码仍旧是只能取得上半部分!!!
panel1.DrawToBitmap(memoryImageSamSung, new Rectangle(0,panel1.Height/2, panel1.Width, panel1.Height/2)); --------------------编程问答-------------------- 怎么不可以呢,楼主再仔细测试下代码 --------------------编程问答-------------------- 每次取得都是 lable6 以上的部分!!! --------------------编程问答-------------------- lable7 以下的部分,怎么取,都取不着呀,!!!!
要不,我给代码你们试试看看!!! --------------------编程问答-------------------- 那就发你相对完整点的代码上来 --------------------编程问答-------------------- Rectangle sourceRect = panel1.ClientRectangle;
Size targetSize = new Size(memoryImageSamSung.Width, memoryImageSamSung.Height);
using (Bitmap tmp = new Bitmap(sourceRect.Width, sourceRect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
{
panel1.DrawToBitmap(tmp, sourceRect);
using (Graphics g = Graphics.FromImage(memoryImageSamSung))
{
g.DrawImage(tmp, 0,0, new Rectangle(0, panel1.Height / 2, panel1.Width, panel1.Height / 2), GraphicsUnit.Pixel);
}
} --------------------编程问答-------------------- 楼上的仍旧 不成,
我的代码很简单的一测试:
Graphics mygraphics = panel1.CreateGraphics();//创建的是整个panel
Size s = panel1.Size;//取panel大小
memoryImageSamSung = new Bitmap(s.Width, s.Height);
panel1.DrawToBitmap(memoryImageSamSung, new Rectangle(0, 150 这个地方设置啥数都没有作用, panel1.Width, panel1.Height / 2));
memoryImageSamSung.Save("f:\\aaa.jpg"); --------------------编程问答-------------------- 大哥们,急解!!! --------------------编程问答-------------------- 方法一
--------------------编程问答--------------------
private void button1_Click(object sender, EventArgs e)
{
Bitmap newBitmap = new Bitmap(panel1.Width, panel1.Height / 2);
Graphics g = Graphics.FromImage(newBitmap);
Point src = PointToScreen(new Point(panel1.Location.X, panel1.Location.Y + panel1.Height / 2));
Point dest = new Point(0, 0);
g.CopyFromScreen(src, dest, new Size(newBitmap.Width, newBitmap.Height), CopyPixelOperation.SourceCopy);
g.Dispose();
newBitmap.Save("d:\\2222.png", System.Drawing.Imaging.ImageFormat.Png);
newBitmap.Dispose();
}
--------------------编程问答-------------------- 方法二:
memoryImageSamSung = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(memoryImageSamSung, new Rectangle(0, 0, this.Width, this.Height));
Bitmap newBitmap = new Bitmap(panel1.Width, panel1.Height);
Graphics mygraphics = Graphics.FromImage(newBitmap);
Rectangle rectangle = new Rectangle(0, this.panel1.Height / 2, panel1.Width, panel1.Height / 2);
Rectangle newRectangle = new Rectangle(0, 0, panel1.Width, panel1.Height);
mygraphics.DrawImage(memoryImageSamSung, newRectangle, rectangle, GraphicsUnit.Pixel);
newBitmap.Save(@"f:\aaa.jpg");
--------------------编程问答-------------------- 智商余额不足/分不够用啊 你们的代码可以通过,
Bitmap newBitmap = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(newBitmap,panel1.ClientRectangle);
Bitmap halfBitmap = newBitmap.Clone(new Rectangle(0,newBitmap.Height>>1,newBitmap.Width,newBitmap.Height>>1),newBitmap.PixelFormat);
newBitmap.Dispose();
halfBitmap.Save("d:\\2222.png", System.Drawing.Imaging.ImageFormat.Png);
halfBitmap.Dispose();
为啥我的代码就不行呢!!!
--------------------编程问答-------------------- 方法三
--------------------编程问答-------------------- 方法很多的,你仔细调试下就知道原因出在哪里了~ --------------------编程问答--------------------
Bitmap newBitmap = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(newBitmap, panel1.ClientRectangle);
Bitmap halfBitmap = new Bitmap(newBitmap.Width, newBitmap.Height >> 1);
Graphics g = Graphics.FromImage(halfBitmap);
g.DrawImage(newBitmap, 0, -halfBitmap.Height);
g.Dispose();
halfBitmap.Save("d:\\2222.png", System.Drawing.Imaging.ImageFormat.Png);
halfBitmap.Dispose();
DrawToBitmap 第二个参数是目标区域表示的矩形,上面的都搞错了(当作源矩形了) --------------------编程问答-------------------- Bitmap bit = new Bitmap(plPMT.Width, plPMT.Height);//实例化一个和窗体一样大的bitmap
Graphics g = Graphics.FromImage(bit);
g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高
//g.CopyFromScreen(this.Left ,this.Top,0,0,new Size(this.Width,this.Height));//保存全部窗体为图片
g.CopyFromScreen(plPMT.PointToScreen(System.Drawing.Point.Empty), System.Drawing.Point.Empty, plPMT.Size);//只保存某个控件(这里是panel游戏区)
bit.Save(@"f:\bbb.jpg");//默认保存格局为PNG,保存成jpg格局质量不是很好
我测试通过了,无论是是panel的背景图片还是自己在panel里绘制的图形都可以截图保存下来!
补充:.NET技术 , C#