本文介绍了WPF抽奖图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,
我已经在台式计算机上开发了一个小程序.
我的配置是
操作系统:Win7 x64
CPU i7 2.7GHz
RAM:4GB

该程序可以在64位和32位的win7下正常工作.也可以在Vista上正常工作.
但是在XP中不是.由于内存不足"而出现错误.


该代码有什么问题.

Rect rec = new Rect();
            rec.Width = ImageDefaults.ImgWidth;
           rec.Height = ImageDefaults.ImgHeight;         

           //基本图片
           BitmapImage格式=新的BitmapImage();
           forma.BeginInit();
          forma.UriSource =新的Uri(defaultPath,UriKind.Relative);
          格式.EndInit();       

           //用作RenderTargetBitmap的源的视觉效果.
            DrawingVisual drawingVisual = new DrawingVisual();
           DrawingContext drawingContext = drawingVisual.RenderOpen();
           drawingContext.DrawImage(forma,new Rect(0,0,forma.Width,forma.Height));

          ;  //上------------------------
          ;  //17 R
           BitmapImage img17 =新的BitmapImage();
            rec.X = middleX +(ext/2);
           rec.Y = middleY +(ext/2);         
           img17.BeginInit();
          img17.UriSource =新的Uri(tiffNamesList32 [16],UriKind.Relative);
img17.EndInit();         
           drawingContext.DrawImage(RotateImage(img17),rec);
           

           //24    R        
           BitmapImage img24 =新的BitmapImage();
            img24.BeginInit();
          img24.UriSource =新的Uri(tiffNamesList32 [23],UriKind.Relative);
img24.EndInit();
          rec.X = middleX-(ext/2)-ImageDefaults.ImgWidth;
            rec.Y = middleY +(ext/2);
           drawingContext.DrawImage(RotateImage(img24),rec);

       
        //-其他图像(还有12张)

            drawingContext.Close();
           //使用Visual渲染的BitmapSource.
           RenderTargetBitmap rtb =新的RenderTargetBitmap(forma.PixelWidth,forma.PixelHeight,300,300,PixelFormats.Default);
rtb.Render(drawingVisual);

            FormatConvertedBitmap newFormatedBitmapSource =新FormatConvertedBitmap();
           newFormatedBitmapSource.BeginInit();
           newFormatedBitmapSource.Source = rtb;
           newFormatedBitmapSource.DestinationFormat = PixelFormats.Cmyk32;
           if(isGrayScale)
              newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray8;
           newFormatedBitmapSource.EndInit();

            //将RenderBitmapTarget编码为PNG文件.
           TiffBitmapEncoder tiff =新的TiffBitmapEncoder();
            tiff.Frames.Add(BitmapFrame.Create(newFormatedBitmapSource));
           tiff.Compression = TiffCompressOption.Lzw;
           字符串名称= string.Format(savePath/*省略*/);
           使用(Stream stm = File.Create(name))
           {
               tiff.Save(stm);
          }

获取异常
rtb.Render(drawingVisual);
在这里!
我多次调用了此方法.第二次是gettin错误.
有时第一次可以正常工作,但第三次是gettin错误.
无法理解这里出了什么问题.
PS:上面提到的函数在类内部,并且类正确实现了IDisposable.

Hello,
I have develop a small program on my desktop computer.
My configuration is
OS: Win7 x64
CPU i7 2.7GHz
RAM : 4GB

the program works fine with win7 both 64 and 32 bit. Also work fine Vista.
But not in XP. Getting error as "insufficent memory"


Whats wrong with this code.

Rect rec = new Rect();
            rec.Width = ImageDefaults.ImgWidth;
            rec.Height = ImageDefaults.ImgHeight;          

            // Base Image
            BitmapImage forma = new BitmapImage();
            forma.BeginInit();
            forma.UriSource = new Uri(defaultPath, UriKind.Relative);
            forma.EndInit();                             

            // The Visual to use as the source of the RenderTargetBitmap.
            DrawingVisual drawingVisual = new DrawingVisual();
            DrawingContext drawingContext = drawingVisual.RenderOpen();
            drawingContext.DrawImage(forma, new Rect(0, 0, forma.Width, forma.Height));

            //On------------------------
            //17 R
            BitmapImage img17 = new BitmapImage();
            rec.X = middleX + (ext / 2);
            rec.Y = middleY + (ext / 2);           
            img17.BeginInit();
            img17.UriSource = new Uri(tiffNamesList32[16], UriKind.Relative);
            img17.EndInit();          
            drawingContext.DrawImage(RotateImage(img17), rec);
           

            //24    R          
            BitmapImage img24 = new BitmapImage();
            img24.BeginInit();
            img24.UriSource = new Uri(tiffNamesList32[23], UriKind.Relative);
            img24.EndInit();
            rec.X = middleX - (ext / 2) - ImageDefaults.ImgWidth;
            rec.Y = middleY + (ext / 2);
            drawingContext.DrawImage(RotateImage(img24), rec);

       
        //--- other images (have 12 more )

            drawingContext.Close();
            // The BitmapSource that is rendered with a Visual.
            RenderTargetBitmap rtb = new RenderTargetBitmap(forma.PixelWidth, forma.PixelHeight, 300, 300, PixelFormats.Default);
            rtb.Render(drawingVisual);

            FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();
            newFormatedBitmapSource.BeginInit();
            newFormatedBitmapSource.Source = rtb;
            newFormatedBitmapSource.DestinationFormat = PixelFormats.Cmyk32;
            if(isGrayScale)
                newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray8;
            newFormatedBitmapSource.EndInit();

            // Encoding the RenderBitmapTarget as a PNG file.
            TiffBitmapEncoder tiff = new TiffBitmapEncoder();
            tiff.Frames.Add(BitmapFrame.Create(newFormatedBitmapSource));
            tiff.Compression = TiffCompressOption.Lzw;
            string name = string.Format(savePath /* omitted */);
            using (Stream stm = File.Create(name))
            {
                tiff.Save(stm);
            }

Getting exception
rtb.Render(drawingVisual);
here!
I m calling this method several times.
sometimes works fine first time but second time gettin error.
sometimes works fine first two times but gettin error third time.
Couldn understand whats wrong here.
PS: Above mentioned function is inside a class and class implements IDisposable properly.

推荐答案


这篇关于WPF抽奖图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 16:20