假设我有非常大的(3000 X 4000)WPF画布。
我正在寻找将此画布切割成一堆n个.png图像切片的最佳方法。
有什么建议吗?
答案 0 :(得分:4)
不确定最好或最简单,但这是一种方式:)
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();
}
答案 1 :(得分:0)
你可以做这样的事情(不是我的代码,取自msdn social):
public static RenderTargetBitmap GetJpgImage(UIElement targetUIElement, double scale, int quality)
{
double actualHeight = targetUIElement.RenderSize.Height;
double actualWidth = targetUIElement.RenderSize.Width;
double renderedHeight = actualHeight * scale;
double renderedWidth = actualWidth * scale;
RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderedWidth, (int)renderedHeight, 96, 96, PixelFormats.Pbgra32);
VisualBrush sourceBrush = new VisualBrush(targetUIElement);
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
using (drawingContext)
{
drawingContext.PushTransform(new ScaleTransform(scale, scale));
drawingContext.DrawRectangle(sourceBrush, null, new Rect(new System.Windows.Point(0, 0), new System.Windows.Point(actualWidth, actualHeight)));
}
renderTarget.Render(drawingVisual);
JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
jpgEncoder.QualityLevel = quality;
jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
MemoryStream memoryStream = new MemoryStream();
jpgEncoder.Save(memoryStream);
File.WriteAllBytes(@"D:\a.jpg", memoryStream.ToArray());
return renderTarget;
}
详细说明将JPG分成正方形的代码,然后可以使用Grid,以编程方式创建一堆列和行,并以编程方式使用图像上的Grid.Row和Grid.Column附加属性来指定(切片)图像存在。