2010-03-23 217 views
0

我正在加载图层图层来制作单个图像。我目前将它们全部堆叠到画布上。我已经设置好了,所以用户可以指定单个图像的最终尺寸,但即使改变画布尺寸,图像也会保留其原始尺寸。WPF图像裁剪

我试图修改图像的大小,因为我正在加载它们,但尺寸是NaN,实际尺寸是0,所以我无法在那里更改它们。

我开始认为画布可能不是要走的路。有关我如何剪裁图像以适合特定尺寸的任何建议?

 canvas1.Children.Clear(); 
     int totalImages = Window1.GetNumberOfImages(); 
     if (drawBackground) 
      canvas1.Background = new SolidColorBrush(Color.FromArgb(a,r,g,b)); 
     else 
      canvas1.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)); 

     for (int i = 0; i < totalImages; i++) 
     { 
      Image image = new Image(); 
      image.Source = Window1.GetNextImage(i); 

      canvas1.Children.Add(image);     
     } 

回答

1

要获得当前的画布尺寸,您必须先致电MeasureArrange。这将避免NaN和0。

使用RenderTransform更改Canvas显示的图像的大小。

我从来没有尝试裁剪图像,所以我不知道该怎么做,但我看到有一个CroppedBitmap对象。我猜你已经试过了?

+0

还没有尝试过,甚至都不知道!一旦我可以回到自己的电脑,我会试一试。 – Califer 2010-03-23 22:16:54

+0

我将for循环的内部更改为以下内容,现在可以使用。 Image image = new Image(); BitmapSource tempSource = Window1.GetNextImage(i); CroppedBitmap CB =新CroppedBitmap(tempSource, 新Int32Rect(0,0, Math.Min((int)的Window1.totalWinWidth,tempSource.PixelWidth) Math.Min((int)的Window1.totalWinHeight, tempSource.PixelHeight))); image.Source = cb; canvas1.Children.Add(image); – Califer 2010-03-24 14:39:41

+0

> _ <我想我不能把代码格式置于评论中...... – Califer 2010-03-24 14:41:05

0

对我来说似乎应该是修改图像源的大小,而不仅仅是画布。画布只是一个容器,它不具有修改其子项的功能。

+0

对,这就是我想要做的。但是,当我加载它们的大小设置为NaN和0. – Califer 2010-03-23 21:29:09

+0

从Window1.GetNextImage(i)返回的对象的类型是什么?那是NaN和0? – 2010-03-23 21:57:20

+0

这是一个BitmapSource。我确信它有适合尺寸的正确信息,但是当我将它加载到图像中时,图像没有正确的尺寸。 – Califer 2010-03-23 22:06:26

3

对于其他正在做同样的事情的人来说,这里是我工作的代码。谢谢Jeffrey!

  Image image = new Image(); 
      BitmapSource tempSource = Window1.GetNextImage(i); 
      CroppedBitmap cb = new CroppedBitmap(tempSource, 
         new Int32Rect(0, 0, 
          Math.Min((int)Window1.totalWinWidth, tempSource.PixelWidth), 
          Math.Min((int)Window1.totalWinHeight, tempSource.PixelHeight))); 
      image.Source = cb; 

      canvas1.Children.Add(image); 
+0

不客气!很高兴我能帮上忙。 :-) – 2010-03-24 14:46:48