将图像添加到WPF表单

编程入门 行业动态 更新时间:2024-10-27 07:15:42
本文介绍了将图像添加到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 ???

推荐答案

你好hanifuk 基本上,您有两项任务要完成.第一个是使ImageSource显示在图像控件中.您可以通过以下方法从System.Drawing.Bitmap或给定的文件或网络路径(名称中包括所有名称空间)创建BitmapImage: Hello hanifuk Basically you have two tasks to accomplish. The first one is getting an ImageSource to display in the image control. Here is a way you could create a BitmapImage either from a System.Drawing.Bitmap or from a given file or network path (included all namespaces in the names): 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="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/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表单

本文发布于:2023-10-10 22:52:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479985.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   图像   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!