Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF:代码隐藏中按钮上的图像字节数组_C#_Wpf - Fatal编程技术网

C# WPF:代码隐藏中按钮上的图像字节数组

C# WPF:代码隐藏中按钮上的图像字节数组,c#,wpf,C#,Wpf,我尝试过很多东西的组合,但没有乐趣 以下是我认为应该有效的方法,但我得到一个空白按钮: ImageConverter ic = new ImageConverter(); System.Drawing.Image img = (System.Drawing.Image)ic.ConvertFrom myByteArray); Bitmap bitmap = new Bitmap(img); MemoryStream ms = new MemoryStream(); ImageBrush br

我尝试过很多东西的组合,但没有乐趣

以下是我认为应该有效的方法,但我得到一个空白按钮:

ImageConverter ic = new ImageConverter();
System.Drawing.Image img = (System.Drawing.Image)ic.ConvertFrom myByteArray);
Bitmap bitmap = new Bitmap(img);

MemoryStream ms = new MemoryStream();

ImageBrush brush = new ImageBrush();

bitmap.Save(ms, ImageFormat.Png);
ms.Position = 0

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CreateOptions = BitmapCreateOptions.None;
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.StreamSource = ms;
bi.EndInit();

brush.ImageSource = bi;
button.Background = brush;
欢迎任何建议。。。这也必须在代码隐藏中完成

编辑: 抱歉,我要补充的是,我使用的是WPF、MVVM方法

此代码适用于一个对话框窗口,但不适用于另一个对话框窗口:

所以我从主窗口打开一个对话框窗口,然后打开另一个对话框窗口,这段代码显示按钮上的图像。。。然而,当我打开第三个对话框窗口,并尝试相同的代码,按钮是空白的

_stockButtons[i].Width = 100;
_stockButtons[i].Height = 100;

ImageBrush brush2 = new ImageBrush();
BitmapImage bitmap2 = new BitmapImage();
bitmap2.BeginInit();
bitmap2.UriSource = new Uri(@"C:\Users\Kevin\Pictures\test.jpg");
bitmap2.EndInit();
brush2.ImageSource = bitmap2;
_stockButtons[i].Background = brush2;
如果我使用上述代码将图像加载到窗口初始加载时的按钮中,图像显示正常。。。问题似乎是当我在左列选择一个按钮时,我显示了与单击的部门相关的项目的按钮数量(1个或更多)。图像似乎丢失在某个地方,或者没有正确地拉入?这有意义吗

XAML:

尝试此操作(不使用WinForms图像)


嗯,我在这方面运气不好afraid@Kev你什么意思?是否有任何错误消息?如何获得myByteArrayarray?我在同一类的公共属性中有字节数组,使用getter从模型实体检索byteArray。我已经检查过了,它就在那里。。。问题是,如果我使用“URISource”获取它工作的测试图片的路径ImageBrush brush=newImageBrush();BitmapImage位图=新的BitmapImage();bitmap.BeginInit();bitmap.UriSource=新Uri(@“C:\Picture.jpg”);EndInit();brush.ImageSource=位图;背景=画笔;抱歉,位图图像中出现错误消息:“BitmapMetadata在BitmapImage上不可用。”添加此行
bitmap2.CacheOption=BitmapCacheOption.OnLoad在您的
BeginInit
EndInit
之间。谢谢NaceRedine。。不幸的是,这不起作用,但我在那里添加了一个屏幕截图来准确显示应该发生什么:)对于这个视图,集合和所有代码都在同一个viewmodel中。我只是将按钮集合绑定到XAML中的一个控件。。。我尝试绑定Button[]数组和ObservableCollections,只是想看看它是否会有所不同。。谢谢again@kev:您可以发布相关的XAML标记吗。我怀疑这个问题可能与我在这里遇到的问题相似,我甚至尝试过一些简单的方法,比如改变背景颜色。。。它甚至不会变色!我可以改变内容好,但颜色甚至不会改变。。。我试着用一个设定器给按钮分配一种新的样式,以获得一种新的画笔颜色,我试着直接改变颜色。。。当窗口已经加载时,动态加载窗口上的按钮似乎是一个问题。。但是我怎样才能改变内容而不是颜色??混乱
<Window x:Class="Views.WindowsViews.SelectStockDialogWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ViewModel="clr-namespace:ViewModels;assembly=ViewModels"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/core"
    xmlns:dxd="http://schemas.devexpress.com/winfx/2008/xaml/docking"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:Support="clr-namespace:Support;assembly=Support"
    xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Royale"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Title="Select stock item"
    mc:Ignorable="d" WindowStartupLocation="CenterScreen" Width="585" Height="600" >

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Views;component/Styles/Brushes.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <dxd:DockLayoutManager Name="dlSalesScreen">
        <dxd:DockLayoutManager.LayoutRoot>
            <dxd:LayoutGroup Name="Root" Orientation="Horizontal" AllowSplitters="False">

                <dxd:LayoutPanel AllowClose="False" AllowRename="False" 
                                 Caption="Departments" HorizontalScrollBarVisibility="Hidden" 
                                 CaptionAlignMode="AutoSize"
                                 CaptionImageLocation="BeforeText" ShowPinButton="False" >

                    <!-- Scrollviewer for department buttons-->
                    <ScrollViewer x:Name="deptScrollviewer" Grid.Row="0" Grid.Column="0" Width="185" Height="Auto" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" PanningMode="VerticalOnly">
                        <ItemsControl ItemsSource="{Binding Departments}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel VerticalAlignment="Top" Height="Auto" Orientation="Vertical" />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
                    </ScrollViewer>
                </dxd:LayoutPanel>

                <dxd:LayoutPanel AllowClose="False" AllowRename="False" 
                                 Caption="Available stock in department" Width="Auto" 
                                 CaptionAlignMode="AutoSize" 
                                 CaptionImageLocation="BeforeText"  ShowPinButton="False">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="50" />
                        </Grid.RowDefinitions>
                        <!-- Scrollviewer for stock buttons-->
                        <ScrollViewer x:Name="stockScrollViewer" Grid.Row="0" Width="Auto" Height="Auto" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" PanningMode="VerticalOnly">
                            <ItemsControl ItemsSource="{Binding StockItems, UpdateSourceTrigger=PropertyChanged}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapPanel VerticalAlignment="Top" HorizontalAlignment="Left" Orientation="Horizontal" Width="400" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                            </ItemsControl>
                        </ScrollViewer>
                        <Rectangle Grid.Row="1" Margin="0,0,0,0" Height="Auto" Fill="{StaticResource BottomRectangleGradient}" />
                        <Grid Name="gridButtonHolder" Grid.Row="2">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <GroupBox x:Name="grpStockItem" Grid.Column="0" Header="Selected Item" HorizontalAlignment="Left" Width="200" >
                                <Label x:Name="lblStockName" Content="{Binding SelectedStockItemLabel}" HorizontalAlignment="Left" />
                            </GroupBox>
                            <Button Name="btnSave" Content="Apply" Command="{Binding ConfirmSelectionCommand}" dxc:ThemeManager.ThemeName="Office2007Blue" Grid.Column="1" Width="110" Height="42" Margin="0,8,0,0" />
                            <Button Name="btnClose" Content="Cancel" dxc:ThemeManager.ThemeName="Office2007Blue" Grid.Column="2" Width="110" Height="42" Margin="0,8,0,0" />
                        </Grid>
                    </Grid>
                </dxd:LayoutPanel>
            </dxd:LayoutGroup>
        </dxd:DockLayoutManager.LayoutRoot>
    </dxd:DockLayoutManager>
</Window>
ImageBrush brush;
BitmapImage bi;
using (var ms = new MemoryStream(myByteArray))
{
    brush = new ImageBrush();

    bi = new BitmapImage();
    bi.BeginInit();
    bi.CreateOptions = BitmapCreateOptions.None;
    bi.CacheOption = BitmapCacheOption.OnLoad;
    bi.StreamSource = ms;
    bi.EndInit();
}

brush.ImageSource = bi;
button.Background = brush;