使用代码将图像对象添加到WPF [英] Adding image objects to wpf with code

查看:54
本文介绍了使用代码将图像对象添加到WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#和WPF的新手,我正在尝试创建一个简单的汽车模拟器.主要是模拟器的想法是,我有C#类来创建汽车对象,这些对象具有例如可以更改的速度变量和从左向右移动的计时器.我想用计时器而不是 doubleanimation 来运动.在WPF中,我具有AddCarButton用于在Canvas中的某些点添加汽车.

I'm a rookie at C# and WPF and I'm trying to create a simple car-simulator. Mainly the idea of the simulator is that I have C#-class that creates car-objects that have for example speed variable that can be changed and timer for moving from left to right. I want to do movement with timer and not for example doubleanimation. In WPF I have AddCarButton for adding cars in certain points in Canvas.

问题是我不知道如何将汽车添加到Canvas.这非常令人沮丧,因为这听起来不算一件大事,但我觉得我已经尝试了一切,但都没有成功.

The problem is I dont know how to add cars to Canvas. This is very frustrating because it doesn't sound like a big thing to do but I feel like I have tried everything and not succeeded.

这是汽车级的最新尝试.我已经尝试过使用Canvas.Set-methods,但是失败了.

This is latest attempt with car-class. I have tried using Canvas.Set-methods but failed.

class car
{
    private int speed;

    public car(int s)
    {
        speed = s;
        Bitmap bmp = new Bitmap(
        System.Reflection.Assembly.GetEntryAssembly().
        GetManifestResourceStream("MyProject.Resources.car.png"));
        Graphics g = Graphics.FromImage(bmp);
        //Canvas.SetBottom(g, 0);
        //Canvas.SetLeft(g, 0);
        //Canvas.SetBottom(bmp, 0);
        //Canvas.SetLeft(bmp, 0);
    }
    public void addCar(car c)
    {
        Canvas.SetBottom(c, 0);
        Canvas.SetLeft(c, 0);
    }

推荐答案

如果您在WPF上进行编码,则不应使用 Windows Forms 东西.要使用图像,请使用 BitmapSource 及其派生类,并以编程方式访问资源,通常使用 pack URIs .不过,这不是唯一的方法.

If you're coding on WPF you shouldn't use Windows Forms stuff. To work with images you use BitmapSource and its derived classes, and to access your resources programmatically you usually use pack URIs. It's not the only way, though.

这是一个在画布控件上绘制一些图像的小例子.

Here is a little example that draws some images on a canvas control.

画布的XAML代码可能是这样的(只是一个例子):

The XAML code for the canvas could be like this (it's just an example):

<Canvas Height="400" HorizontalAlignment="Left" Margin="0" Name="canvas1" VerticalAlignment="Top" Width="400" />

和您的主窗口代码...

and your main window code...

public partial class MainWindow : Window
{
    BitmapImage carBitmap = new BitmapImage(new Uri("pack://application:,,,/Images/BlueCar.png", UriKind.Absolute));
    Image[] carImg = new Image[5];
    Random rnd = new Random();

    public MainWindow()
    {
        InitializeComponent();
        double maxX = canvas1.Width - carBitmap.Width;
        double maxY = canvas1.Height - carBitmap.Height;
        for (int i = 0; i < carImg.Length; i++)
        {
            carImg[i] = new Image();
            carImg[i].Source = carBitmap;
            carImg[i].Width = carBitmap.Width;
            carImg[i].Height = carBitmap.Height;
            Canvas.SetLeft(carImg[i], rnd.NextDouble() * maxX);
            Canvas.SetTop(carImg[i], rnd.NextDouble() * maxY);
            canvas1.Children.Add(carImg[i]);
        }
    }
}

显然,您需要更改图像资源的名称.顺便说一句,要添加图像,请进入项目> 添加现有项目... 并选择您的图像文件,现在您的图像将出现在 Solution Explorer中(默认情况下,Visual Studio将图像资源存储在名为图像"的文件夹中),如果选择它,则会在 Properties 窗口中看到其 Build操作资源 请勿更改! (有些人认为它应该是嵌入式资源,但这是错误).

Obviously you need change the name of your image resource. By the way, to add an image go to Project > Add existing item... and select your image file, now your image will appear in the Solution explorer (by default, Visual Studio stores image resources in a folder called "Images"), if you select it you'll see in the Properties window that its Build action is Resource, don't change this! (some people think it should be Embedded resource but that's incorrect).

如果您没有获得此新Uri("pack://application:,,,/Images/BlueCar.png",UriKind.Absolute),则应阅读 pack URIs 上的"http://msdn.microsoft.com/en-us/library/aa970069%28v=VS.85%29.aspx" rel ="nofollow">此链接>.

If you don't get this new Uri("pack://application:,,,/Images/BlueCar.png", UriKind.Absolute), you should read this link on pack URIs.

这篇关于使用代码将图像对象添加到WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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