WinForm控件与WPF控件的交互
这个问题其实也可以理解为:怎样在WPF/XAML中使用Winform中的控件(如PictureBox)?
首先看看XAML代码:(注意下面加粗的部分)
<Window x:Class="WindowsApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WinForm控件与WPF控件的交互" Height="400" Width="600"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
>
<StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Source="Girl.jpg" MaxHeight="300" Grid.Column="0" Name="WPFImage"/>
<WindowsFormsHost Grid.Column="1" MaxHeight="300" Name="pictureHost" >
<wf:PictureBox />
</WindowsFormsHost>
</Grid>
</StackPanel>
</Window>
C#代码:
System.Windows.Forms.PictureBox _pictureBox = null;
void Window1_Loaded(object sender, RoutedEventArgs e)
{
_pictureBox = pictureHost.Child as System.Windows.Forms.PictureBox;
_pictureBox.Image = GetBitmap(WPFImage);
}
#region Image Functions
// 以下代码实现了两者WPF与GDI+的交互
public void ConvertToGrayscale(System.Drawing.Bitmap source , int sliderVal)
{
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(source.Width, source.Height);
//下面代码还可以使用不安全代码,以提高效率
for (int y = 0; y < bm.Height; y++)
{
for (int x = 0; x < bm.Width; x++)
{
System.Drawing.Color c = source.GetPixel(x, y);
int luma = (int)(c.R * ((double)sliderVal / (double)10) * 0.3 + c.G * ((double)sliderVal / (double)10) * 0.59 + c.B * ((double)sliderVal / (double)10) * 0.11);
bm.SetPixel(x, y, System.Drawing.Color.FromArgb(luma, luma, luma));
}
}
_pictureBox.Image = bm;
}
System.Drawing.Bitmap AdjustBrightnessMatrix(System.Drawing.Bitmap img, int value)
{
if (value == 0) // No change, so just return
return img;
float sb = (float)value / 255F;
float[][] colorMatrixElements =
{
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {sb, sb, sb, 1, 1}
};
System.Drawing.Imaging.ColorMatrix cm = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
System.Drawing.Imaging.ImageAttributes imgattr = new System.Drawing.Imaging.ImageAttributes();
System.Drawing.Rectangle rc = new System.Drawing.Rectangle(0, 0, img.Width, img.Height);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
imgattr.SetColorMatrix(cm);
g.DrawImage(img, rc, 0, 0, img.Width, img.Height, System.Drawing.GraphicsUnit.Pixel, imgattr);
imgattr.Dispose();
g.Dispose();
return img;
}
#endregion Image Functions
#region Image-Bitmap Interop Helpers
private void convertBitmapToBitmapSource(System.Drawing.Bitmap bitmap)
{
using (bitmap)
{
System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bitmap.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
WPFImage.Source = bitmapSource;
}
}
private System.Drawing.Bitmap GetBitmap(Image image)
{
System.Windows.Forms.PictureBox picture = _pictureBox;
// Stream stm = File.Open("Waterfall.jpg", FileMode.Open, FileAccess.Read))
//// Since we're not specifying a System.Windows.Media.Imaging.BitmapCacheOption, the pixel format
//// will be System.Windows.Media.PixelFormats.Pbgra32.
// System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Media.Imaging.BitmapFrame.Create(
// stm,
// System.Windows.Media.Imaging.BitmapCreateOptions.None,
// System.Windows.Media.Imaging.BitmapCacheOption.OnLoad);
System.Windows.Media.Imaging.BitmapSource bitmapSource = WPFImage.Source as BitmapSource;
// Scale the image so that it will display similarly to the WPF Image.
double newWidthRatio = picture.Width / (double)bitmapSource.PixelWidth;
double newHeightRatio = ((picture.Width * bitmapSource.PixelHeight) / (double)bitmapSource.PixelWidth) / (double)bitmapSource.PixelHeight;
System.Windows.Media.Imaging.BitmapSource transformedBitmapSource = new System.Windows.Media.Imaging.TransformedBitmap(
bitmapSource,
new System.Windows.Media.ScaleTransform(newWidthRatio, newHeightRatio));
int width = transformedBitmapSource.PixelWidth;
int height = transformedBitmapSource.PixelHeight;
int stride = width * ((transformedBitmapSource.Format.BitsPerPixel + 7) / 8);
byte[] bits = new byte[height * stride];
transformedBitmapSource.CopyPixels(bits, stride, 0);
unsafe
{
fixed (byte* pBits = bits)
{
IntPtr ptr = new IntPtr(pBits);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(
width,
height,
stride,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb,
ptr);
return bitmap;
}
}
}
#endregion Image-Bitmap Interop Helpers
想像一下,通过相互转换、相互调用,可以很方便地实现一些功能(彼此功能互补嘛)。
参考资源:
Gotchas For Working With Windows Forms/WPF Interop http://blogs.msdn.com/scoberry/archive/2006/09/01/735844.aspx
WinForm控件与WPF控件的交互的更多相关文章
- 关于WinForm引用WPF窗体---在Winform窗体中使用WPF控件
项目中有个界面展示用WPF实现起来比较简单,并且能提供更酷炫的效果,但是在WinForm中使用WPF窗体出现了问题,在网上找了一下有些人说Winform不能引用WPF的窗体,我就很纳闷,Win32都能 ...
- 在Winform窗体中使用WPF控件(附源码)
原文:在Winform窗体中使用WPF控件(附源码) 今天是礼拜6,下雨,没有外出,闲暇就写一篇博文讲下如何在Winform中使用WPF控件.原有是我在百度上搜索相关信息无果,遂干脆动手自己实现. W ...
- 在WinForm应用程序中嵌入WPF控件
我们知道,在WPF界面上添加WinForm的控件需要使用WindowsFormHost类.而在WinForm界面上添加WPF控件该如何做呢?有没有类似的类呢?明显是有的,ElementHost就是为了 ...
- WPF控件相对位置解析
WPF控件相对位置的获取方法是比较简单的.对于初学者来说,掌握这一技巧的应用,可以帮助以后对WPF的深入学习,而且在实际使用中,这是一个非常常用的方法. 我们知道WPF有着比较灵活的布局方式,关于某个 ...
- 通过WinForm控件创建的WPF控件无法输入的问题
今天把写的一个WPF程序发布到别的机器上执行,发现一个比较奇怪的问题:在那个机器上用英文输入法无法输入数字,非要切换到中文输入法才行:但在我的机器上却是好好的. 最开始以为是输入法的问题,弄了好一阵子 ...
- Wpf使用Winform控件后Wpf元素被Winform控件遮盖问题的解决
有人会说不建议Wpf中使用Winform控件,有人会说建议使用Winform控件在Wpf下的替代方案,然而在实际工作中由于项目的特殊需求,考虑到时间.成本等因素,往往难免会碰到在WPF中使用Winfr ...
- WPF 精修篇 Winform 嵌入WPF控件
原文:WPF 精修篇 Winform 嵌入WPF控件 首先 创建WPF控件库 这样就有了一个WPF界面 在wpf中增加界面等 在winform中增加WPFDLL 重新生成解决方案 在左侧工具栏 出现W ...
- WPF控件--利用Winform库中的NotifyIcon实现托盘小程序
WPF控件--NotifyIcon 运行界面如下所示: 图1 图2 代码很少,如下所示 ...
- 解决 CefSharp WPF控件不能使用输入法输入中文的问题(代码已提交到 github)
首先,本文所有 代码已经提交到github,需要的可以直接从github获取:https://github.com/starts2000/CefSharp,希望可以帮助到有需要的朋友们. CEF 简介 ...
随机推荐
- storm集群操作指南
目录 storm集群操作指南 一.storm伪分布式安装 (一)环境准备 (二)安装zookeeper (三)安装storm (四)运行程序 二.storm集群安装 (一)下载storm并解压 (二) ...
- go 保留小数若干位数
感谢 https://blog.csdn.net/sjy8207380/article/details/79013827 解决的方法 · 利用取近似值的方法解决这个问题. (1)利用fmt.Sprin ...
- ZOJ 1796 Euchre Results 数学水题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1796 题意: 四个人玩游戏,已知三个人的输赢情况,求第四个人的输赢情况. ...
- POJ 1163 The Triangle 简单DP
看题传送门门:http://poj.org/problem?id=1163 困死了....QAQ 普通做法,从下往上,可得状态转移方程为: dp[i][j]= a[i][j] + max (dp[i+ ...
- [NIO]用dawn发送接收HTTP请求
HTTP协议的下层使用的是tcp.所以我们建立一个tcp连接就能发送接收http请求.dawn底层使用了nio.可是经过dawn的封装之后,我们在编写代码的时候,就和使用普通的堵塞式socket一样 ...
- 关于用strace工具定位vrrpd进程有时会挂死的bug
只做工作总结备忘之用. 正在烧镜像,稍总结一下进来改bug遇到的问题. 一个项目里要用到L3 switch的nat,vrrp功能,但实地测试中偶然出现write file挂死的情况,但不是必现.交付在 ...
- iOS调试 - 基本技巧
在程序中,无论是你想弄清楚为什么数组中有3个对象而不是5个,或者为什么一个新的玩家开始之后,游戏在倒退——调试在这些处理过程中是比较重要的一部 分.通过本文的学习,我们将知道在程序中,可以使用的大多数 ...
- 【48.47%】【POJ 2524】Ubiquitous Religions
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 32364 Accepted: 15685 Description There a ...
- USB 3.0规范中译本 附录
本文为CoryXie原创译文,转载及有任何问题请联系cory.xie#gmail.com. 附录A 符号编码 表A-1显示了对于数据字符字节到符号的编码. 表 A-2显示了对于特殊符号的编码. R ...
- Docker CE for Windows安装使用
原文:Docker CE for Windows安装使用 官网下载并安装Docker CE for Windows IDEA连接Docker Docker一些常用命令 Docker for windo ...