Es muy común que nos veamos en la necesidad de cambiar el tamaño de una imagen antes de, por ejemplo, subirla a la web. Para esto, hay muchos métodos que podemos utilizar, unos más sencillos o con más capacidades que otros.

Para aquellas veces que sólo ocupamos cambiar el tamaño de la imagen manteniendo sus proporciones, WPF nos facilita el trabajo con las propiedades DecodePixelWidth o DecodePixelHeight de la clase BitmapImage.

El siguiente método es todo lo que ocupamos para lograrlo:

 
        public static BitmapImage ResizeImage(BitmapImage image, int newSize)
        {
            BitmapImage i = new BitmapImage();
            i.BeginInit();
            i.UriSource = image.UriSource;
            i.DecodePixelWidth = newSize;
            i.EndInit();
            return i;
        }

De aquí es importante notar tres cosas:

Al método le pasamos una imagen y un nuevo tamaño. El nuevo tamaño se asigna a la propiedad DecodePixelWidth que será la nueva anchura de la imagen.

La altura de la imagen se deja sin asignar para que se ajuste automáticamente y la imagen mantenga sus proporciones.

El método regresa una nueva imagen con el tamaño deseado.

Con esta imagen podemos hacer lo que queramos, por ejemplo, con el siguiente código podemos guardarla en formato JPG en algún lugar de la pc:

 
        public static void SaveDecodeToJPG(string filename, BitmapImage image)
        {
            FileStream stream = new FileStream(filename, FileMode.Create);
            JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
            jpgEncoder.Frames.Add(BitmapFrame.Create(image));
            jpgEncoder.Save(stream);
            stream.Close();
        }

Baja un ejemplo de este código funcionando aquí.

Ok, this is my very first post in English and it will be very short, I just hope’s that it helps you to understand a new way to use the data binding.

What will we do?

I will use a VERY simple application to show how you can bind a property of an object to another object, binding the length of the text in a TextBox to the text in a TextBlock. So that the TextBlock will show the number of characters in the TextBox automatically as you type. You can see the functional sample here

The application only has two controls: a TextBox and a TextBlock:

image

The XAML for that goes like this:

<Grid Background="White">
    	<TextBlock Margin="0,0,14,8" VerticalAlignment="Bottom" Height="32" Text="{Binding Text.Length, ElementName=theText}" TextWrapping="Wrap" FontSize="24" HorizontalAlignment="Right" Width="124" Foreground="#FF767474" TextAlignment="Right"/>
    	<TextBox Margin="17,8,14,8" x:Name="theText" Text="TextBox" TextWrapping="Wrap" FontSize="24" Background="{x:Null}" BorderBrush="Black"/>
    </Grid>

As you can see, we have assigned a Name to the TextBox, this is important because we need to tell the Binding from where to get the values. Now to the interesting part, the binding:

Set the TextBlock text value to {Binding Text.Length, ElementName=theText}, so you have something like this:

<TextBlock Margin="0,0,14,8" VerticalAlignment="Bottom" Height="32" Text="{Binding Text.Length, ElementName=theText}" TextWrapping="Wrap" FontSize="24" HorizontalAlignment="Right" Width="124" Foreground="#FF767474" TextAlignment="Right"/>

With this, we have already finished the sample application,  if you run it, you will see a control like this one:

image

Binding is a very powerful tool that can be used in many ways and that can save a lot of work, like in the sample above; Binding allowed us to set an “interaction” betwenn two objects without having to write any code at all.