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 简介 ...
随机推荐
- css3-13 css3的3D动画如何实现
css3-13 css3的3D动画如何实现 一.总结 一句话总结:这里是transform+setInterval实现.transform属性里面的rotate属性值变成rotateX或rotateY ...
- MySQL启动关闭添加到 /etc/init.d/mysqld
cp /data/mysql/support-files/mysql.server /etc/init.d/mysqld 然后就可以使用此命令启动/关闭 mysql: /etc/ini ...
- HINTERNET应包含的头文件
#include <afxinet.h> DWORD status=0;DWORD dwLen=sizeof(DWORD); if(!HttpQueryInfo(hRequest,HTTP ...
- HDU 1010 Tempter of the Bone (ZOJ 2110) DFS+剪枝
传送门: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1010 ZOJ:http://acm.zju.edu.cn/onlinejudge/showPr ...
- github-vimium-compile-crx
chrome-62.x版本上安装vimium 1.61.1 https://blog.csdn.net/ZHUJIANWEILI4/article/details/78385346
- ebook https://salttiger.com/category/notification/
https://salttiger.com/category/notification/ 因为个人精力有限,持续搜集.整理.分享电子书已经占用我大部分业余时间,今后不再给评论区留言的朋友们找书,还希望 ...
- 【t053】整数去位
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 键盘输入一个高精度的正整数N,去掉其中任意M个数字后剩下的数字按原左右次序将组成一个新的正整数.编程对 ...
- java中关于File类的mkdirs()和FIle()构造方法在windows环境内网测试总结
环境:环境是内网windows,传入路径的时候不带盘符,并且服务器的win10也不是超级管理员账户 关于File类的mkdirs()远程访问创建不了文件和路径的问题. 如果浏览器操作者是windows ...
- 【Samza系列】实时计算Samza中文教程(四)—API概述
上一篇和大家一起宏观上学习了Samza平台的架构,重点讲了一下数据缓冲层和资源管理层.剩下的一块非常重要的SamzaAPI层本节作为重点为大家展开介绍. 当你使用Samza来实现一个数据流处理 ...
- 获取WebConfig 配置项的值
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...