WPF How to center the Image.Source

27,587

Solution 1

Set Stretch to None.

<Image Source="..." Stretch="None" />

Solution 2

You should try and center the whole Image element itself using the alignments :

    <Grid>
        <Image Stretch="None"
               HorizontalAlignment="Center"
               VerticalAlignment="Center" />
    </Grid>
Share:
27,587
JoanComasFdz
Author by

JoanComasFdz

BS Computer Science @ EPSEVG (UPC BarcelonaTech) .NET C#, WPF / SL, WCF, TDD, DDD. SOreadytohelp

Updated on July 10, 2022

Comments

  • JoanComasFdz
    JoanComasFdz almost 2 years

    I am developing a custom Image control in WPF .NET 3.5 and Visual Studio 2010.

    In WinForms the PicutreBox control has the SizeMode property which includes "CenterImage".

    I want my Image control to have that ability.

    Is there anyway?

    Thanks

    My XAML code:

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="768" Width="1024" xmlns:my="http://schemas.sharpsoft.net/xaml" xmlns:my1="clr-namespace:WpfApplication1">
        <Grid>
            <my1:CustomControl1
                        x:Name="customControl11"
                        Width="206"
                        Height="197"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        Margin="18,58,0,0"
                        Stretch="Uniform"/>
        </Grid>
    </Window>
    

    My CustomControl code:

    public class CustomControl1 : Image
    {
        public CustomControl1()
        {
            // Bitmap to Stream
            Stream ms = new MemoryStream();
            Properties.Resources.webcam_background.Save(ms, ImageFormat.Png);
    
            // Stream to BitmapImage
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.StreamSource = ms;
            bitmap.EndInit();
    
            // Set it
            Source = bitmap;
        }
    }
    

    Where "webcam_backgroud" is a png image added by default visual studio resource editor.

  • JoanComasFdz
    JoanComasFdz almost 14 years
    I've tryed, but it is'nt getting the effect i would expect. By now it reduces my image a lot and put it on the upper left corner. None: img28.imageshack.us/img28/1783/imagestretchnone.png Uniform: img810.imageshack.us/img810/2401/imagestretchuniform.png
  • Quartermeister
    Quartermeister almost 14 years
    @unexpectedkas: Can you edit your question to include some of your XAML? The image will be drawn centered in the Image control by default, but the Image control may not be centered or stretched across its parent. For example, if your entire window is a Grid with one Image then it will be centered, but if it's a Canvas with one Image then it will be in the upper-left corner.
  • JoanComasFdz
    JoanComasFdz almost 14 years
    Ok, I have just did it. I setted H/V align to center and, yes, the image centers. Thanks a lot. Anyway to look it right Stretch must be "Uniform". I was confused because the control moved on window ¬¬'
  • JoanComasFdz
    JoanComasFdz almost 14 years
    Yes, I did it. but for some reason the control would present a kind of "thumbnail", not the original one. I'm thinking that is because of the way I am loding a resource, that is no using URIs.