gpt4 book ai didi

wpf - 将大的 WPF Canvas 切成许多小的 PNG 图像

转载 作者:行者123 更新时间:2023-12-04 14:38:18 25 4
gpt4 key购买 nike

假设我有非常大的 (3000 X 4000) WPF Canvas 。

我正在寻找将此 Canvas 分割成一堆 n x n .png 图像 block 的最佳方法。

有什么建议吗?

最佳答案

不确定最好还是最简单,但这里有一个方法 :)

private void SaveFrameworkElement(FrameworkElement frameworkElement)
{
BitmapImage bitmapImage = VisualToBitmapImage(frameworkElement);
for (int startX = 0; startX <= 400; startX += 100)
{
for (int startY = 0; startY <= 100; startY += 100)
{
SaveImage(bitmapImage, startX, startY, 100, 100, "C:\\CutImage_" + startX.ToString() + "-" + startY.ToString() + ".png");
}
}
}

public BitmapImage VisualToBitmapImage(FrameworkElement frameworkElement)
{
RenderTargetBitmap rtb = new RenderTargetBitmap((int)frameworkElement.ActualWidth,
(int)frameworkElement.ActualHeight,
96d,
96d,
PixelFormats.Default);
rtb.Render(frameworkElement);

MemoryStream stream = new MemoryStream();
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(stream);

BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();

return bitmapImage;
}
public void SaveImage(BitmapImage sourceImage,
int startX,
int startY,
int width,
int height,
string filePath)
{
TransformGroup transformGroup = new TransformGroup();
TranslateTransform translateTransform = new TranslateTransform();
translateTransform.X = -startX;
translateTransform.Y = -startY;
transformGroup.Children.Add(translateTransform);

DrawingVisual vis = new DrawingVisual();
DrawingContext cont = vis.RenderOpen();
cont.PushTransform(transformGroup);
cont.DrawImage(sourceImage, new Rect(new Size(sourceImage.PixelWidth, sourceImage.PixelHeight)));
cont.Close();

RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Default);
rtb.Render(vis);

FileStream stream = new FileStream(filePath, FileMode.Create);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(stream);
stream.Close();
}

关于wpf - 将大的 WPF Canvas 切成许多小的 PNG 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4453059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com