wpf 导出canvas 保存为图片.

时间:2022-08-08 06:36:43

由于图片尺寸过大 大概是 20000x15000 所以在 
RenderTargetBitmap bmp = new RenderTargetBitmap((int)this.content.ActualWidth, (int)this.content.ActualHeight, 96, 96, PixelFormats.Pbgra32);
的时候提示内存不够
尝试过了GC.Collect();GC.WaitForPendingFinalizers();回收一些垃圾
但是还是不够。
想问一下有没有什么办法在不改变图片尺寸的情况下导出来?

5 个解决方案

#1


有人知道么?只要能导出来就行。本来想切成多片导出。但是在分配内存的时候就出错。就很无奈了。
求高手指点啊!

#2


~~~~~没有人么?

#3


我也碰到这样的问题,录求解决方法

#4


找到另外的解决方法了,使用切片分幅导出

#5


 double imgWith = element.Width * 300 / 96;
            double imgHeith = element.Height * 300 / 96;
            if (imgWith <= 3508 && imgHeith<=2480) //A4
            {
                imgWith = 3508;
                imgHeith = 2480;
                element.Width = 1122;//3508 * 96 / 300;
                element.Height = 793;//2480 * 96 / 300;
            }
            else if (imgHeith <= 2480 && imgWith >3508) // 横向
            {
                element.Height = 793;
                imgHeith = 2480;
            }
            else if (imgHeith > 2480 && imgWith <= 3508) //纵向
            {
                element.Width = 1122;
                imgWith = 3508;
            }
            else if (imgHeith > 2480 && imgWith > 3508)//横向 纵向
            {
                double HeithRate = Math.Ceiling((int)imgHeith / (double)2480);
                double WidthRate = Math.Ceiling((int)imgWith / (double)3508);

                element.Height = 793 * HeithRate;
                element.Width = 1122 * WidthRate;
            }
            //imgWith = 4961; //a3
            //imgHeith = 3508;

            RenderTargetBitmap bmp = new RenderTargetBitmap(3508, 2480, 300d, 300d, PixelFormats.Pbgra32);
            //重新刷新
            //Size size = new Size(element.Width, element.Height);
            Size size = new Size( 1122,793);
            element.Measure(size);
            element.Arrange(new Rect(new Point(){ X=0, Y=0 }, size));
            bmp.Render(element);
            return bmp;
        }

        public void ExmportImage(FrameworkElement element, string fileName )
        {

            
            double imgWith = element.Width * 300 / 96;
            double imgHeith = element.Height * 300 / 96;
            if (imgWith <= 3508 && imgHeith <= 2480) //A4
            {
                element.Width = 1122;//3508 * 96 / 300;
                element.Height = 793;//2480 * 96 / 300;
                RenderTargetBitmap bmp = new RenderTargetBitmap(3508, 2480, 300d, 300d, PixelFormats.Pbgra32);
                //重新刷新
                Size size = new Size(1122, 793);
                element.Measure(size);
                element.Arrange(new Rect(new Point() { X = 0, Y = 0 }, size));
                bmp.Render(element);

                WriteableBitmap wbitmap = new WriteableBitmap(bmp);
                ExportImage.SaveWriteableBitmapToBmpImage(wbitmap, fileName);
            }
            else if (imgHeith <= 2480 && imgWith > 3508) // 横向
            {
                double WidthRate = Math.Ceiling((int)imgWith / (double)3508);
                element.Height = 793;
                element.Width = 1122 * WidthRate;

                for (int i = 1; i <= WidthRate; i++)
                {
                    //重新刷新
                    RenderTargetBitmap bmp = new RenderTargetBitmap(3508, 2480, 300d, 300d, PixelFormats.Pbgra32);
                    Size size = new Size(1122*i, 793);
                    element.Measure(size);
                    element.Arrange(new Rect(new Point() { X =-(size.Width-1122) , Y = 0 }, size));
                    bmp.Render(element);
                    string fName = string.Format(fileName.Replace(".jpg","")+"({0}).jpg" , i);
                    WriteableBitmap wbitmap = new WriteableBitmap(bmp);
                    ExportImage.SaveWriteableBitmapToBmpImage(wbitmap, fName);
                    bmp = null;
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
            else if (imgHeith > 2480 && imgWith <= 3508) //纵向
            {
                double HeithRate = Math.Ceiling((int)imgHeith / (double)2480);
                element.Width = 1122;
                element.Height = 793 * HeithRate;

                for (int i = 1; i <= HeithRate; i++)
                {
                    RenderTargetBitmap bmp = new RenderTargetBitmap(3508, 2480, 300d, 300d, PixelFormats.Pbgra32);
                    Size size = new Size(1122, 793*i);
                    element.Measure(size);
                    element.Arrange(new Rect(new Point() { X =0, Y = -(size.Height-793) }, size));
                    bmp.Render(element);
                    string fName = string.Format(fileName.Replace(".jpg","")+"({0}).jpg" , i);
                    WriteableBitmap wbitmap = new WriteableBitmap(bmp);
                    ExportImage.SaveWriteableBitmapToBmpImage(wbitmap, fName);
                    bmp = null;
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
            else if (imgHeith > 2480 && imgWith > 3508)//横向 纵向
            {
                double HeithRate = Math.Ceiling((int)imgHeith / (double)2480);
                double WidthRate = Math.Ceiling((int)imgWith / (double)3508);

                element.Height = 793 * HeithRate;
                element.Width = 1122 * WidthRate;

                for (int y = 1; y <= HeithRate; y++)
                {
                    for (int x = 1; x <= WidthRate; x++)
                    {
                        RenderTargetBitmap bmp = new RenderTargetBitmap(3508, 2480, 300d, 300d, PixelFormats.Pbgra32);
                        Size size = new Size(1122*x, 793 * y);
                        element.Measure(size);
                        element.Arrange(new Rect(new Point() { X = -(size.Width-1122), Y = -(size.Height - 793) }, size));
                        bmp.Render(element);
                        string fName = string.Format(fileName.Replace(".jpg", "") + "({0}-{1}).jpg", x,y);
                        WriteableBitmap wbitmap = new WriteableBitmap(bmp);
                        ExportImage.SaveWriteableBitmapToBmpImage(wbitmap, fName);
                        bmp = null;
                        GC.Collect(); 
                        GC.WaitForPendingFinalizers();
                    }
                }
            }

#1


有人知道么?只要能导出来就行。本来想切成多片导出。但是在分配内存的时候就出错。就很无奈了。
求高手指点啊!

#2


~~~~~没有人么?

#3


我也碰到这样的问题,录求解决方法

#4


找到另外的解决方法了,使用切片分幅导出

#5


 double imgWith = element.Width * 300 / 96;
            double imgHeith = element.Height * 300 / 96;
            if (imgWith <= 3508 && imgHeith<=2480) //A4
            {
                imgWith = 3508;
                imgHeith = 2480;
                element.Width = 1122;//3508 * 96 / 300;
                element.Height = 793;//2480 * 96 / 300;
            }
            else if (imgHeith <= 2480 && imgWith >3508) // 横向
            {
                element.Height = 793;
                imgHeith = 2480;
            }
            else if (imgHeith > 2480 && imgWith <= 3508) //纵向
            {
                element.Width = 1122;
                imgWith = 3508;
            }
            else if (imgHeith > 2480 && imgWith > 3508)//横向 纵向
            {
                double HeithRate = Math.Ceiling((int)imgHeith / (double)2480);
                double WidthRate = Math.Ceiling((int)imgWith / (double)3508);

                element.Height = 793 * HeithRate;
                element.Width = 1122 * WidthRate;
            }
            //imgWith = 4961; //a3
            //imgHeith = 3508;

            RenderTargetBitmap bmp = new RenderTargetBitmap(3508, 2480, 300d, 300d, PixelFormats.Pbgra32);
            //重新刷新
            //Size size = new Size(element.Width, element.Height);
            Size size = new Size( 1122,793);
            element.Measure(size);
            element.Arrange(new Rect(new Point(){ X=0, Y=0 }, size));
            bmp.Render(element);
            return bmp;
        }

        public void ExmportImage(FrameworkElement element, string fileName )
        {

            
            double imgWith = element.Width * 300 / 96;
            double imgHeith = element.Height * 300 / 96;
            if (imgWith <= 3508 && imgHeith <= 2480) //A4
            {
                element.Width = 1122;//3508 * 96 / 300;
                element.Height = 793;//2480 * 96 / 300;
                RenderTargetBitmap bmp = new RenderTargetBitmap(3508, 2480, 300d, 300d, PixelFormats.Pbgra32);
                //重新刷新
                Size size = new Size(1122, 793);
                element.Measure(size);
                element.Arrange(new Rect(new Point() { X = 0, Y = 0 }, size));
                bmp.Render(element);

                WriteableBitmap wbitmap = new WriteableBitmap(bmp);
                ExportImage.SaveWriteableBitmapToBmpImage(wbitmap, fileName);
            }
            else if (imgHeith <= 2480 && imgWith > 3508) // 横向
            {
                double WidthRate = Math.Ceiling((int)imgWith / (double)3508);
                element.Height = 793;
                element.Width = 1122 * WidthRate;

                for (int i = 1; i <= WidthRate; i++)
                {
                    //重新刷新
                    RenderTargetBitmap bmp = new RenderTargetBitmap(3508, 2480, 300d, 300d, PixelFormats.Pbgra32);
                    Size size = new Size(1122*i, 793);
                    element.Measure(size);
                    element.Arrange(new Rect(new Point() { X =-(size.Width-1122) , Y = 0 }, size));
                    bmp.Render(element);
                    string fName = string.Format(fileName.Replace(".jpg","")+"({0}).jpg" , i);
                    WriteableBitmap wbitmap = new WriteableBitmap(bmp);
                    ExportImage.SaveWriteableBitmapToBmpImage(wbitmap, fName);
                    bmp = null;
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
            else if (imgHeith > 2480 && imgWith <= 3508) //纵向
            {
                double HeithRate = Math.Ceiling((int)imgHeith / (double)2480);
                element.Width = 1122;
                element.Height = 793 * HeithRate;

                for (int i = 1; i <= HeithRate; i++)
                {
                    RenderTargetBitmap bmp = new RenderTargetBitmap(3508, 2480, 300d, 300d, PixelFormats.Pbgra32);
                    Size size = new Size(1122, 793*i);
                    element.Measure(size);
                    element.Arrange(new Rect(new Point() { X =0, Y = -(size.Height-793) }, size));
                    bmp.Render(element);
                    string fName = string.Format(fileName.Replace(".jpg","")+"({0}).jpg" , i);
                    WriteableBitmap wbitmap = new WriteableBitmap(bmp);
                    ExportImage.SaveWriteableBitmapToBmpImage(wbitmap, fName);
                    bmp = null;
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
            else if (imgHeith > 2480 && imgWith > 3508)//横向 纵向
            {
                double HeithRate = Math.Ceiling((int)imgHeith / (double)2480);
                double WidthRate = Math.Ceiling((int)imgWith / (double)3508);

                element.Height = 793 * HeithRate;
                element.Width = 1122 * WidthRate;

                for (int y = 1; y <= HeithRate; y++)
                {
                    for (int x = 1; x <= WidthRate; x++)
                    {
                        RenderTargetBitmap bmp = new RenderTargetBitmap(3508, 2480, 300d, 300d, PixelFormats.Pbgra32);
                        Size size = new Size(1122*x, 793 * y);
                        element.Measure(size);
                        element.Arrange(new Rect(new Point() { X = -(size.Width-1122), Y = -(size.Height - 793) }, size));
                        bmp.Render(element);
                        string fName = string.Format(fileName.Replace(".jpg", "") + "({0}-{1}).jpg", x,y);
                        WriteableBitmap wbitmap = new WriteableBitmap(bmp);
                        ExportImage.SaveWriteableBitmapToBmpImage(wbitmap, fName);
                        bmp = null;
                        GC.Collect(); 
                        GC.WaitForPendingFinalizers();
                    }
                }
            }