none
生成图片时,报内存不足,不知何故 RRS feed

  • 问题

  •  public void CreatGoodImg(int int_Width, int int_Height, string intput_ImgFile, string out_ImgFile)
        {
            System.Drawing.Image OldImage = System.Drawing.Image.FromFile(intput_ImgFile);
            int New_Width, New_Height, Old_Width, Old_Height;
            int flat, xPoint, yPoint;
            flat = 0;
            xPoint = 0;
            yPoint = 0;
            Old_Width = OldImage.Width;
            Old_Height = OldImage.Height;
            //缩略图片宽与高(与模板同比缩放)
            if ((Old_Width / Old_Height) > (int_Width / int_Height))
            {
                New_Width = int_Width;
                New_Height = Old_Height * (int_Width / Old_Width);
                yPoint = (int_Height - New_Height) / 2;
                flat = 1;
            }
            else if ((Old_Width / Old_Height) == (int_Width / int_Height))
            {
                New_Width = int_Width;
                New_Height = int_Height;
            }
            else
            {
    
                New_Width = Old_Width * (int_Height / Old_Height);
                New_Height = int_Height;
                xPoint = (int_Width - New_Width) / 2;
                flat = 1;
            }
            System.Drawing.Image thumbnailImage = OldImage.GetThumbnailImage(New_Width, New_Height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
            Bitmap bm = new Bitmap(thumbnailImage);
    	try{
            if (flat != 0)
            {
                Bitmap bmOutPut = new Bitmap(int_Width, int_Height);
                Graphics g = System.Drawing.Graphics.FromImage(bmOutPut);
                SolidBrush tbbg = new SolidBrush(Color.White);
                g.FillRectangle(tbbg, 0, 0, int_Width, int_Height);
                g.DrawImage(bm, xPoint, yPoint, New_Width, New_Height);
                bmOutPut.Save("E:" + out_ImgFile);
            }
            else
            {
                bm.Save("E:" + out_ImgFile);
            }
    	}catch(Exception ex){}
            bm.Dispose();
    	OldImage.Dispose();
    	thumbnailImage.Dispose();
        }
    2010年3月18日 13:21

答案

  • 你好!
         实际上操作系统对于Bitmap的大小是由限制的,具体可以参考这个帖子:
    http://social.msdn.microsoft.com/Forums/zh-TW/233/thread/b9d8ff6c-9717-4227-80e1-e4a79ac453ee
         希望对你有帮助!

    周雪峰
    • 已标记为答案 YiChun Chen 2010年3月22日 11:38
    2010年3月18日 14:09
    版主