转换为WPF [英] Converting to WPF

查看:83
本文介绍了转换为WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将较旧的Window Forms应用程序转换为WPF,但我坚持使用这一功能.它将图像放入PictureBox控件中,抓取图像的特定部分,然后将图像的该部分移到另一个控件中.

I am trying to convert an older Window Forms application to WPF, and I am stuck on this one function. It takes the image in a PictureBox control, grabs a specific section of it, and moves that section of the image to another control.

无论如何,这是旧代码.谁能将其转换为在WPF中为我工作?我有很多这样的功能,但是一旦我有了需要做的基础,就可以完成其余的功能.

Anyway, here is the old code. Can anyone please convert this to work in WPF for me? I have many functions like this, but once I get the foundation of what I need to do, I'll be able to finish the rest of the functions.

        int sx = (selectedPaletteTile % paletteColumns) * 33;
        int sy = (selectedPaletteTile / paletteColumns) * 33;
        Rectangle src = new Rectangle(sx, sy, 32, 32);
        Rectangle dst = new Rectangle(0, 0, 32, 32);
        Graphics gfxSelected;
	gfxSelected.DrawImage(pictureBoxPalette.Image, dst, src, GraphicsUnit.Pixel);
        pictureBoxSelected.Image = selectedBitmap;

我的问题是新的System.Windows.Shapes.Rectangle类不再具有用于设置位置/大小的构造函数,并且我也找不到Graphics类.所以我真的很想转换.

My problem is that the new System.Windows.Shapes.Rectangle class no longer has a constructor for setting the location/size, and I cannot find the Graphics class either. So I'm really stuck on converting this.

谢谢.

推荐答案

使用DrawingContext

Use DrawingContext

using System.Drawing;
public void DrawRectangle()
        {

           DrawingVisual drawingVisual = new DrawingVisual();

           // Retrieve the DrawingContext in order to create new drawing content.
           DrawingContext drawingContext = drawingVisual.RenderOpen();

            /* Get the Image From File */
            BitmapImage image = new BitmapImage(new Uri(@"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg"));
            

            int sx = (selectedPaletteTile % paletteColumns) * 33;
            int sy = (selectedPaletteTile / paletteColumns) * 33;
            
            /* Gets a portion of Image from the sx,sy postions for the specified height and width */
            CroppedBitmap imageCropped = new CroppedBitmap(image, new Int32Rect(sx, sy, 32, 32));

            /* Draw the image on scrren */
            drawingContext.DrawImage(imageCropped, new System.Windows.Rect(0, 0, 32, 32));
        }


这篇关于转换为WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆