簡體   English   中英

WPF:后面的代碼中按鈕上的圖像的字節數組

[英]WPF: Byte array to Image on a button in code behind

圖像控件不包含圖像。

我嘗試了許多事情的組合,但沒有喜悅。

這是我認為應該工作的,但是我得到了一個空白按鈕:

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;

如果我使用上面的代碼將圖像加載到窗口的初始加載按鈕上,則圖像顯示為OK。問題似乎是當我在左列中選擇一個按鈕時,我顯示了一個金額( 1個或更多)與所單擊部門相關的項目的按鈕。 圖像似乎在某處丟失或未正確拉入? 這有意義嗎?

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>

試試看(不使用WinForms圖像)

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM