【问题标题】:Bind Xaml bitmap image绑定 Xaml 位图图像
【发布时间】:2012-06-30 18:07:59
【问题描述】:

我有位图图像变量,我想将它绑定到我的 xaml 窗口。

System.Reflection.Assembly thisExe;
        thisExe = System.Reflection.Assembly.GetExecutingAssembly();
        string[] resources = thisExe.GetManifestResourceNames();
        var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("SplashDemo.Resources.Untitled-100000.png");
        Bitmap image = new Bitmap(stream);

这是我的 xaml 代码

<Image Source="{Binding Source}" HorizontalAlignment="Left"  Height="210" Margin="35,10,0,0" VerticalAlignment="Top" Width="335">
    </Image>

您能帮我通过 C# 代码将此位图变量绑定到此 xaml 图像吗?

【问题讨论】:

    标签: c# xaml bitmap bind


    【解决方案1】:

    如果你真的想从 C# 代码而不是从 XAML 内部设置它,你应该使用这个简单的解决方案described further on the MSDN reference

    string path = "Resources/Untitled-100000.png";
    BitmapImage bitmap = new BitmapImage(new Uri(path, UriKind.Relative));
    image.Source = bitmap;
    

    但首先,您需要为您的 Image 命名,以便您可以从 c# 中引用它:

    <Image x:Name="image" ... />
    

    无需引用 Windows 窗体类。 如果您坚持将图像嵌入到您的程序集中,则需要以下更冗长的代码来加载图像:

    string path = "SplashDemo.Resources.Untitled-100000.png";
    using (Stream fileStream = GetType().Assembly.GetManifestResourceStream(path))
    {
        PngBitmapDecoder bitmapDecoder = new PngBitmapDecoder(fileStream,
            BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
        ImageSource imageSource = bitmapDecoder.Frames[0];
        image.Source = imageSource;
    }
    

    【讨论】:

    • 重点是我想做一个定时器,像幻灯片一样改变这张图片。我做了一个计时器,但我不知道如何通过 C# 代码更改图像
    • 我发布的代码应该在这种情况下工作。你需要一个例子吗?
    • 是的,但是如果图像嵌入到程序集中呢?
    • 如果您想将BuildAction 设为EmbeddedResource,我找到了解决方案。发帖。
    • 这很好用,感谢队友帮助我。我花了大约 4 个小时来做​​这件事(我缺乏经验),你救了我的大脑。
    【解决方案2】:

    这里是一些示例代码:

    // Winforms Image we want to get the WPF Image from...
    System.Drawing.Image imgWinForms = System.Drawing.Image.FromFile("test.png");
    
    // ImageSource ...
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    MemoryStream ms = new MemoryStream();
    
    // Save to a memory stream...
    imgWinForms.Save(ms, ImageFormat.Bmp);
    
    // Rewind the stream...    
    ms.Seek(0, SeekOrigin.Begin);
    
    // Tell the WPF image to use this stream...
    bi.StreamSource = ms;
    bi.EndInit();
    

    Click here to view reference

    【讨论】:

    • 不客气,在这个论坛上感谢的方式是赞成或接受作为答案。
    • 如果有更直接和简单的方法,为什么要使用MemoryStream 和引用System.Drawing.Image
    【解决方案3】:

    如果您使用 WPF,请右键单击项目中的图像并将 Build Action 设置为 Resource。假设您的图像名为MyImage.jpg 并且位于项目的Resources 文件夹中,您应该能够直接在xaml 中引用它,而无需使用任何C# 代码。像这样:

    <Image Source="/Resources/MyImage.jpg" 
        HorizontalAlignment="Left"
        Height="210" 
        Margin="35,10,0,0"
        VerticalAlignment="Top"
        Width="335">
    </Image>
    

    【讨论】:

    • 不要吹毛求疵,但 OP 确实特别询问了如何在 C# 中完成此操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多