C#中怎样在image中添加图片?

C#中怎样在image中添加图片?,第1张

private void pictureBox1_Click(object sender, EventArgs e)
{
openFileDialog1Filter = "jpg,jpeg,bmp,gif,ico,png,tif,wmf|jpg;jpeg;bmg;gif;ico;png;tif;wmf";
thisopenFileDialog1ShowDialog();
if (thisopenFileDialog1FileNameTrim() == "")
{
return;
}
try
{
picname = openFileDialog1FileName;
pictureBox1Image = ImageFromFile(picname);
}
catch
{
MessageBoxShow(this, "打开图像文件错误!", "信息提示", MessageBoxButtonsOK, MessageBoxIconInformation);
}
}
这段代码是以前照着书上的例子写的,有看不懂的地方追问一下。

imageSource = BitmapFrameCreate(Stream, BitmapCreateOptionsNone, BitmapCacheOptionDefault);
到我的资源去下一个,Image 与 2(3)维数组的转换。再转换成流就很简单了

1、使用DrawingVisual绘制你的内容:
var drawingVisual = new DrawingVisual();
using(var drawingContext = drawingVisualRenderOpen())
{
var text = new FormattedText();
var position = new Point();
drawingVisualDrawText(text, position);
}
2、用RenderTargetBitmap渲染成位图:
var renderTargetBitmap = new RenderTargetBitmap();
renderTargetBitmapRender(drawingVisual);
3、选用一种BitmapEncoder保存,比如PngBitmapEncoder:
var encoder = new PngBitmapEncoder();
var frame = BitmapFrameCreate(renderTargetBitmap);
encoderFramesAdd(frame);
encoderSave();

具体方法跟代码有啥区别。
具体方法就是把图当成背景填入Rectangle。
具体一点就是用Rectangle的Fill属性。
再具体一点的就是,用VisualBrush的Visual装载Image,然后把VisualBrush填充到Rectangle的Fill里面去。
如果这些都不知道的话,那么请去买一本WPF Program,看看再说。

wpf的经典之处在于binding数据,而且是数据驱动事件,一般你用 treeview的话肯定不是像winform一样做的,wpf要用到HierarchicalDataTemplate,里面可以加StackPanel ,当然StackPanel 里就该加需要的元素或控件了,wpf中的treeview没有像winform里那样自带的图标属性,需要自己添加,用<image />标签添加。

可以看一下这个网页如何给treeview添加内容,我就是照着它做的,虽然里面有点小问题,但大体思路就是这样。
嗯,我也是新手,希望可以帮到你。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12801758.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-27
下一篇2025-08-27

发表评论

登录后才能评论

评论列表(0条)

    保存