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 简介 ...
随机推荐
- thinkphp模型事件(钩子函数:模型中在增删改等操作前后自动执行的事件)
thinkphp模型事件(钩子函数:模型中在增删改等操作前后自动执行的事件) 一.总结 1.通过模型事件(钩子函数),可以在插入更新删除等前后执行一些特定的功能 2.模型事件是写在模型里面的,控制器中 ...
- 【例题3-1 UVA - 272 】TEX Quotes
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 大水题. 用一个int记录遇到的是左括号还是右括号. [错的次数] 在这里输入错的次数 [反思] 在这里输入反思 [代码] #inc ...
- 【u248】交通序列号
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 在一条笔直的道路上共有N个路口,每个路口处都有关于该条道路的通行的信号灯. 显然,信号灯共有绿(G). ...
- keil快捷键
- angular项目国际化配置(ngx-translate)
原文 https://www.jianshu.com/p/7d1da3098625 大纲 1.认识ngx-translate 2.ngx-translate的配置步骤 3.ngx-translate的 ...
- .net下载优酷1080P视频
事实上流程大致是:调用飞驴下载API+js解析+文件下载+调用flvBind合并这样一个流程而已_(:з」∠)_ 貌似是不用太多的说明..嗯.. 起先的需求是从优酷上下载一些视频 只是网络上的各种软件 ...
- C++中回调(CallBack)的使用方法(其实就是类方法指针,我觉得你的方法易用性不好,虽然原理正确)
回调函数是一个很有用,也很重要的概念.当发生某种事件时,系统或其他函数将会自动调用你定义的一段函数.回调函数在windows编程使用的场合很多,比如Hook回调函数:MouseProc,GetMsgP ...
- Activity“ 阻止自动弹出软键盘”的方法 -尤其是对于Tab页下的!
我的activity是Tab页签下的~! 所以应把代码加在继承于TabActivity的那个activity中!!而不是由点击Tab页启动的那个activity <activity androi ...
- html5和html的区别是什么(精问)
html5和html的区别是什么(精问) 一.总结 一句话总结:html5:简洁(文档生命,链接引入) 语义化(语义化标签) API(canvas,地理位置等) 一些标签(input新类型) 二. ...
- tensorflow 的版本差异与变化
官方文档的说明,Transitioning to TensorFlow 1.0 1. 简单列举如下 变量 tf.VARIABLES ⇒ tf.GLOBAL_VARIABLES tf.all_varia ...