1

I have one column in my datagrid which contains Icons. for this I have one celltemplate added to column programatically.

var imageFactory = new FrameworkElementFactory(typeof(System.Windows.Controls.Image));
                    imageFactory.SetBinding(System.Windows.Controls.Image.SourceProperty, imageBinding);
                    imageFactory.SetValue(System.Windows.Controls.Image.StretchProperty, Stretch.None);
                    if (config.Font != null)
                    {
                        double height = config.Font.Size;
                        imageFactory.SetValue(FrameworkElement.HeightProperty, height);

                    }
                    var dataTemplate = new DataTemplate { VisualTree = imageFactory };
                    statusColumn.CellTemplate = dataTemplate;
                    view.DataGrid.Columns.Add(statusColumn);

when I set Height property externally it crops the image instead of resizing image to 'height' value.

how to set image height to specific value. please suggest.

deathrace
  • 908
  • 4
  • 22
  • 48

2 Answers2

2

try this

    double size = 14.0;
    BitmapImage bmp = new BitmapImage(new Uri("MyIcon.ico", UriKind.RelativeOrAbsolute));

    FrameworkElementFactory icon = new FrameworkElementFactory(typeof(Image));
    icon.SetValue(Image.SourceProperty, bmp);
    icon.SetValue(Image.WidthProperty, size);
    icon.SetValue(Image.HeightProperty, size);

UPDATE try this

   Style sBase = (Style)this.Resources["BaseButtonStyle"];
   Style sNew = new Style(typeof(Image), sBase);
   sNew.Setters.Add(new Setter(HeightProperty, 20d));

REFERENCE See this

Amol
  • 1,431
  • 2
  • 18
  • 32
  • I have tried same above, setting HeightPrtoperty. but still doesn't work – deathrace Mar 19 '13 at 09:10
  • actually my problem is Icon image is going to be loaded dynamically any time during execution. so I just have FrameworkElementFactory for now at time of Loading. now, I tried to setimageFactory.SetValue(FrameworkElement.HeightProperty, height); but it didn't work. it crops dynamically loeaded image icon. – deathrace Mar 19 '13 at 12:24
  • Worked for me and my related question is: https://stackoverflow.com/questions/59379995/show-image-in-dynamically-generated-datagrid-columns-in-wpf/59390358#59390358 – Will Marcouiller Dec 18 '19 at 16:27
0

I used BitmapImage.DecodePixelHeight and it solved my problem. :)

bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memoryStream;
                bitmapImage.DecodePixelHeight = font.Size <= 9 ? font.Size + 2 : font.Size;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                bitmapImage.Freeze();
deathrace
  • 908
  • 4
  • 22
  • 48