0

Image control contains no image.

I have tried many combinations of things but with no joy.

Here is what i think should work, but I get a blank button:

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;

Any suggestions welcome... this has to be done in the code behind also.

EDIT:

Sorry, just to add that I am using WPF, MVVM approach.

This code works on one dialog window but NOT on another:

So I open a dialog window from the main window and then open another dialog window, this code shows the image on the buttons fine... however when i open a 3rd dialog window, and try the same code, the buttons are blank...

_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;

If I use the above code to load the images into the buttons on the initial load of the window, the images are displayed OK... The issue seems to be when I select a button on the left column, then I display an amount (1 or more) of buttons for the items related to the clicked department. The image seems to be lost somewhere or not being pulled in correctly? Does that makes sense?

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>
4
  • add this line bitmap2.CacheOption = BitmapCacheOption.OnLoad; between your BeginInit and EndInit. Commented Sep 19, 2012 at 10:40
  • thanks Nacereddine.. didnt work unfortunately but i added a screen shot there to show exactly what should be happening :) Both collections and all code is in the same viewmodel for this view. And im simply binding the button collections to a control in the XAML... Ive tried binding to Button[] array and ObservableCollections just to see if it would make a difference .. Thanks again Commented Sep 19, 2012 at 11:33
  • @kev: Can you post the relevant XAML markup. I have a suspicion the problem may be similar to the one I had here stackoverflow.com/questions/6177550/… Commented Sep 19, 2012 at 11:43
  • I have even tried something simple like changing the background colour... it wont even change colour! I can change the content ok but the colour wont even change... I tried assigning a new style to the button with a setter to get a new brush colour and i tried changing the colour directly... it seems to be an issue with loading the buttons dynamically on the window when the window has been loaded already.. but how could i change the content and not the colour?? COnfussssssssssed Commented Sep 19, 2012 at 13:46

1 Answer 1

2

Try this (without using the WinForms images)

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;
Sign up to request clarification or add additional context in comments.

9 Comments

Mmmm no luck on that im afraid
@Kev What do you mean ? Is there any error messages ? How do you obtain you myByteArray array ?
I have the byte array in a public property in the same class, using a getter to retrieve the byteArray from a model entity. I have checked it and its there... Thing is if I use 'URISource' to get the path of a test picture it works
ImageBrush brush = new ImageBrush(); BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri(@"C:\Picture.jpg"); bitmap.EndInit(); brush.ImageSource = bitmap; button.Background = brush;
Sorry there is an error message in the bitmap image : "BitmapMetadata is not available on BitmapImage."
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.