从代码添加图像

本文关键字:图像 添加 代码 | 更新日期: 2023-09-27 18:36:14

好的,这应该很简单。我已经看到了其他几个关于这个问题的问题,它应该有效,但莫名其妙地没有。所以我有那个图像

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

我想通过代码设置它。所以我有一个 s/r StrPath2ResizedBmpSize,它可以调整大小并获得一个 BitmapImage。这是有效的,因为如果在调试中我看一下 bmp 没关系。

然后我想通过这个例程让图像设置徽标图像:

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

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

但它不起作用,并且图像是具有空参数的图像。问题出在哪里?谢谢

从代码添加图像

你应该这样做:

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