原文: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控件的交互的更多相关文章

  1. 关于WinForm引用WPF窗体---在Winform窗体中使用WPF控件

    项目中有个界面展示用WPF实现起来比较简单,并且能提供更酷炫的效果,但是在WinForm中使用WPF窗体出现了问题,在网上找了一下有些人说Winform不能引用WPF的窗体,我就很纳闷,Win32都能 ...

  2. 在Winform窗体中使用WPF控件(附源码)

    原文:在Winform窗体中使用WPF控件(附源码) 今天是礼拜6,下雨,没有外出,闲暇就写一篇博文讲下如何在Winform中使用WPF控件.原有是我在百度上搜索相关信息无果,遂干脆动手自己实现. W ...

  3. 在WinForm应用程序中嵌入WPF控件

    我们知道,在WPF界面上添加WinForm的控件需要使用WindowsFormHost类.而在WinForm界面上添加WPF控件该如何做呢?有没有类似的类呢?明显是有的,ElementHost就是为了 ...

  4. WPF控件相对位置解析

    WPF控件相对位置的获取方法是比较简单的.对于初学者来说,掌握这一技巧的应用,可以帮助以后对WPF的深入学习,而且在实际使用中,这是一个非常常用的方法. 我们知道WPF有着比较灵活的布局方式,关于某个 ...

  5. 通过WinForm控件创建的WPF控件无法输入的问题

    今天把写的一个WPF程序发布到别的机器上执行,发现一个比较奇怪的问题:在那个机器上用英文输入法无法输入数字,非要切换到中文输入法才行:但在我的机器上却是好好的. 最开始以为是输入法的问题,弄了好一阵子 ...

  6. Wpf使用Winform控件后Wpf元素被Winform控件遮盖问题的解决

    有人会说不建议Wpf中使用Winform控件,有人会说建议使用Winform控件在Wpf下的替代方案,然而在实际工作中由于项目的特殊需求,考虑到时间.成本等因素,往往难免会碰到在WPF中使用Winfr ...

  7. WPF 精修篇 Winform 嵌入WPF控件

    原文:WPF 精修篇 Winform 嵌入WPF控件 首先 创建WPF控件库 这样就有了一个WPF界面 在wpf中增加界面等 在winform中增加WPFDLL 重新生成解决方案 在左侧工具栏 出现W ...

  8. WPF控件--利用Winform库中的NotifyIcon实现托盘小程序

    WPF控件--NotifyIcon   运行界面如下所示:            图1                                             图2 代码很少,如下所示 ...

  9. 解决 CefSharp WPF控件不能使用输入法输入中文的问题(代码已提交到 github)

    首先,本文所有 代码已经提交到github,需要的可以直接从github获取:https://github.com/starts2000/CefSharp,希望可以帮助到有需要的朋友们. CEF 简介 ...

随机推荐

  1. Java与模式:装饰(Decorator)模式

    装饰模式使用被装饰类的一个子类的实例.把client的调用委派到被装饰类,装饰模式的关键在于这样的扩展是全然透明的.   装饰模式在Java种使用也非常广泛,比方我们在又一次定义button.对话框等 ...

  2. kafka集群操作指南

    目录 kafka集群操作指南 (一)单机版安装 (二)集群安装 (三)集群启停操作 (四)topic相关的操作 (五)某个broker挂掉,本机器可重启 (六)某个broker挂掉且无法重启,需要其它 ...

  3. Maven基础教程 分类: C_OHTERS 2015-04-10 22:53 232人阅读 评论(0) 收藏

    更多内容请参考官方文档:http://maven.apache.org/guides/index.html 官方文档很详细,基本上可以查找到一切相关的内容. 另外,快速入门可参考视频:孔浩的maven ...

  4. keil出现蓝色小箭头

  5. python中有关字符串的处理

    原文 Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字 ...

  6. hadoop配置文件的加载机制 分类: A1_HADOOP 2015-01-21 11:29 839人阅读 评论(0) 收藏

    hadoop通过Configuration类来保存配置信息 1.通过Configuration.addResource()来加载配置文件 2.通过Configuration.get***()来获取配置 ...

  7. 【33.20%】【LA 4320】【Ping pong】

    [Description] N (3 ≤ N ≤ 20000) ping pong players live along a west-east street(consider the street ...

  8. Centos6.5 VNC 配置

    VNC 配置 本教程配置说明 目的:实现Window下VNC登陆Linux服务器 本机是在root用户下操作完毕 两台主机已实现基本网络配置,且相互ping通 1. 检查图形界面 确保服务器端须要执行 ...

  9. Opencv Sift算子特征提取与匹配

    SIFT算法的过程实质是在不同尺度空间上查找特征点(关键点),用128维方向向量的方式对特征点进行描述,最后通过对比描述向量实现目标匹配. 概括起来主要有三大步骤: 1.提取关键点: 2.对关键点附加 ...

  10. 常用JS验证函数总结

    JS验证Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/- ...