将图像添加到WPF表单

将图像添加到WPF表单

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

问题描述

大家好,
我正在使用wpf应用程序,其中有一个按钮和一个图像控件.每当我单击按钮时,我都希望图片出现在表单上.
然后我希望它可以用鼠标自由移动.

这对我来说似乎很困难:(任何人都可以通过以下代码帮助我吗?

Hello every one,
I am working on a wpf application, where i have a button and an image control. when ever i click on the button i want a picture to appear on the form.
and after i want it to move with mouse freely.

this looks difficult to me :( can any body help me with the code ???

推荐答案

private System.Windows.Media.Imaging.BitmapImage GetBitmapFromFile(string path)
{
    System.Windows.Media.Imaging.BitmapImage img = new System.Windows.Media.Imaging.BitmapImage();
    img.BeginInit();
    // Every path can be represented by an URI
    img.UriSource = new System.Uri(path, System.UriKind.RelativeOrAbsolute);
    img.EndInit();

    return img;
}

private System.Windows.Media.Imaging.BitmapImage GetBitmapFromGDI(System.Drawing.Bitmap bmp)
{
    var strm = new System.IO.MemoryStream();
    // We temporary save the bitmap to a stream inside the memory
    bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Bmp);
    strm.Position = 0;
    System.Windows.Media.Imaging.BitmapImage img = new System.Windows.Media.Imaging.BitmapImage();
    img.BeginInit();
    // The BitmapImage should be loaded from the stream inside memory
    img.StreamSource = strm;
    img.EndInit();

    return img;
}



第一个函数从文件源加载BitmapImage,而第二个函数从System.Drawing.Bitmap加载.获得BitmapImage后,可以使用Source成员将其设置为Imagecontrol的源.要使其跟随鼠标,您可以使用MouseMove事件跟踪鼠标在窗口内的移动.

以下代码显示了如何实现它.假定您的项目中有一个.resX,其中存储了名为Lava_cracks的图像资源,但是您可以更改代码以完全满足您的目的:

MainWindow.cs:



The first functions loads a BitmapImage from a file source while the second one loads it from a System.Drawing.Bitmap. Once you have the BitmapImage you can set it as source for you Imagecontrol using the Source-member. To let it follow the mouse you can keep track of the mouse movement inside the window using the MouseMove event.

The following code shows how to achieve it. It assumes that you have a .resX in your project where an image resource named Lava_cracks is stored but you can change the code simply to fit your purposes:

MainWindow.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ImageFollow
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void image1_Loaded(object sender, RoutedEventArgs e)
        {
            // replace it with what ever you like
            // for example image1.Source = GetBitmapFromFile("myImage.img");
            image1.Source = GetBitmapFromGDI(Resource1.Lava_Cracks);
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            // first we take the coordinates of the mouse relative to the top left corner of our window
            var pos = e.GetPosition(this);
            // then we set the margin of the image control so that its top left corner is at the position
            image1.Margin = new Thickness(pos.X, pos.Y, 0, 0);
        }

        private System.Windows.Media.Imaging.BitmapImage GetBitmapFromFile(string path)
        {
            System.Windows.Media.Imaging.BitmapImage img = new System.Windows.Media.Imaging.BitmapImage();
            img.BeginInit();
            // Every path can be represented by an URI
            img.UriSource = new System.Uri(path, System.UriKind.RelativeOrAbsolute);
            img.EndInit();

            return img;
        }

        private System.Windows.Media.Imaging.BitmapImage GetBitmapFromGDI(System.Drawing.Bitmap bmp)
        {
            var strm = new System.IO.MemoryStream();
            // We temporary save the bitmap to a stream inside the memory
            bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Bmp);
            strm.Position = 0;
            System.Windows.Media.Imaging.BitmapImage img = new System.Windows.Media.Imaging.BitmapImage();
            img.BeginInit();
            // The BitmapImage should be loaded from the stream inside memory
            img.StreamSource = strm;
            img.EndInit();
            strm.Dispose();

            return img;
        }
    }
}



和MainWindow.xaml



and MainWindow.xaml

<Window x:Class="ImageFollow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="482" Width="691" MouseMove="Window_MouseMove">
    <Grid>
        <Image
            Height="150"
            HorizontalAlignment="Left"
            Margin="22,22,0,0"
            Name="image1"
            Stretch="Fill"
            VerticalAlignment="Top"
            Width="200"
            Loaded="image1_Loaded"/>

    </Grid>
</Window>


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

08-22 22:55