C#winform:将2个面板保存为一个图像 [英] C# winform :save 2 panel as one image

查看:91
本文介绍了C#winform:将2个面板保存为一个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将2个面板保存为一个图像?目前,这是我的代码,但它显示错误:无法从system.window.form.panel转换为system.drawing.image。



什么我试过了:



位图位图=新位图(cap_top.Width + cap_btm.Width,Math.Max(cap_top。高度,cap_btm.Height)); 
使用(Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(cap_top,0,0);
g.DrawImage(cap_btm,cap_top.Width,0);

}

解决方案

嗯......错误信息非常清楚。看看DrawImage的文档,确实期望第一个参数是一个Image,而不是一个Control,这就是一个Panel。



所以,小组控件不能在那里使用。你如何为Panel控件提供某种形象?如果它们是Bitmap对象,你只需将它们传递给DrawImage调用,而不是Panel控件。


错误是非常自我解释的。使用类比:

Quote:

你不能在一个圆孔中安装方形钉。



此链接会向您展示如何使用一个面板: c# - 在面板上绘图,另存为位图 - 堆栈溢出 [ ^ ]



要用两个面板来做,首先要在保存之前首先在另一个上绘制一个位图。


Control.DrawToBitmap方法(位图,矩形)(System.Windows.Forms) [ ^ ]?

位图位图= 位图(cap_top.Width + cap_b tm.Width,Math.Max(cap_top.Height,cap_btm.Height)); 
cap_top.DrawToBitmap(位图,矩形( 0 0 ,cap_top.Width,cap_top.Height));
cap_btm.DrawToBitmap(位图,矩形(cap_top.Width, 0 ,cap_btm.Width ,cap_btm.Height));


How do i actually save 2 panel as one image ? currently, this is my code but it shown error : "cannot convert from system.window.form.panel to system.drawing.image" .

What I have tried:

Bitmap bitmap = new Bitmap(cap_top.Width + cap_btm.Width, Math.Max(cap_top.Height, cap_btm.Height));
using (Graphics g = Graphics.FromImage(bitmap))
{
    g.DrawImage(cap_top, 0, 0);
    g.DrawImage(cap_btm, cap_top.Width, 0);

}

解决方案

Ummm...the error message is quite clear. Looking at the documentation for DrawImage, it does indeed expect that first parameter to be an Image, NOT a Control, which is what a Panel is.

So, the Panel controls cannot be used there. How are you supplying an image of some kind to the Panel controls? If they are Bitmap objects, you just pass those in to the DrawImage calls, not the Panel controls.


The error is pretty self explanatory. TO use an analogy:

Quote:

You can not fit a square peg in a round hole.


This Link however shows you how to do it with one panel: c# - Draw on Panel, save as Bitmap - Stack Overflow[^]

To do it with two Panels would be to first paint one bitmap over the other before saving.


How about Control.DrawToBitmap Method (Bitmap, Rectangle) (System.Windows.Forms)[^] ?

Bitmap bitmap = new Bitmap(cap_top.Width + cap_btm.Width, Math.Max(cap_top.Height, cap_btm.Height));
cap_top.DrawToBitmap(bitmap, new Rectangle(0, 0, cap_top.Width, cap_top.Height));
cap_btm.DrawToBitmap(bitmap, new Rectangle(cap_top.Width, 0, cap_btm.Width, cap_btm.Height));


这篇关于C#winform:将2个面板保存为一个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆