【问题标题】:WPF image croppingWPF 图像裁剪
【发布时间】:2011-01-31 00:12:02
【问题描述】:

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

我在加载图像时尝试修改图像大小,但大小为 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);                
        }

【问题讨论】:

    标签: c# wpf image canvas


    【解决方案1】:

    在我看来,您应该修改图像源的大小,而不仅仅是画布。 Canvas 只是一个容器,它没有修改其子项的能力。

    【讨论】:

    • 对,这就是我想要做的。但是,当我加载它们时,它们的大小设置为 NaN 和 0。
    • Window1.GetNextImage(i) 返回的对象的类型是什么?那是 NaN 和 0 的那个?
    • 这是一个 BitmapSource。我确定它具有正确的尺寸信息,但是当我将其加载到图像中时,图像的尺寸不正确。
    【解决方案2】:

    要获取画布的当前大小,您必须先调用MeasureArrange。这将避免 NaN 和 0。

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

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

    【讨论】:

    • 还没试过,甚至都不知道!一旦我可以回到自己的电脑上,我会尝试一下。
    • 我将 for 循环的内部更改为以下内容,它现在可以工作了。图像图像 = 新图像(); 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);
    【解决方案3】:

    对于其他做同样事情的人,这是我开始工作的代码。谢谢杰弗里!

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多