0

OK this should be an easy one. I've seen several other questions about that and it should work but inexplicably it doesn't. So I have that Image

 <Image Name="LogoImage" Width="50" Height="50"> 

and I want to set it through code. So I have a s/r StrPath2ResizedBmpSize which resizes and gets a BitmapImage. That works because if in debug I take a look at the bmp it's ok.

Then I want to get the Image to set the LogoImage through this routine:

LogoImage = ImageUtilities.StrPath2ResizedImgSize(strFilename, 50, 50);

with

public static Image StrPath2ResizedImgSize(string strPath, int newWidth, int newHeight)
{
  var bmp = new BitmapImage();
  bmp = BitmapUtilities.StrPath2ResizedBmpSize(strPath, newWidth, newHeight);
  Image img = new Image();
  img.Source = bmp;<-----image is not set and it's null
  return img;
}

but it doesn't work and the image is an image with null parameters. Where is the problem? Thank you

2
  • 1
    Why are you instanciating a new Image object ? Why not just set the source of the LogoImage one ? (which is already instanciated) Commented Feb 25, 2016 at 12:55
  • 1
    what's BitmapUtilities. Commented Feb 25, 2016 at 12:56

1 Answer 1

0

You should do like this:

public static void StrPath2ResizedImgSize(Image img, string strPath, int newWidth, int newHeight)
{
  img.Source = BitmapUtilities.StrPath2ResizedBmpSize(strPath, newWidth, newHeight);
}

ImageUtilities.StrPath2ResizedImgSize(LogoImage, strFilename, 50, 50);
Sign up to request clarification or add additional context in comments.

2 Comments

So from what I see and from Sidewinder's comment I see that the problem was that I was instatiating a new Image. Fine that works . But what I can't understand is why creating a new Image and then returning it makes it not working. In my book what I was doing shouild be bad practice not wrong. Any enlightment is very appreciated thanx
If you add that new Image to a grid for example, it will show the bitmap. If you want replace this new img with the old img, you have to remove old img from the UI, then add new img to replace.

Your Answer

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