2009-06-23 75 views
0

我正在尝试裁剪图像。我已经找到了多种方法来做到这一点,但是没有人执行我想要的。一旦图像被剪切,我将它发送到PDF生成器。如果我发送正常的JPG,它可以正常工作,但是如果我剪切图像,它不会以正确的大小显示在PDF上。我认为这可能与解决方案有关。以C保存图像保留分辨率#

它在html视图中看起来很好,但是当它发布为PDF时,图像比预期的要小。

这里是我使用的裁剪代码:

  try 
     { 
      System.Drawing.Image image = System.Drawing.Image.FromFile(img); 
      Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb); 
      bmp.SetResolution(image.HorizontalResolution, image.VerticalResolution); 

      Graphics gfx = Graphics.FromImage(bmp); 
      gfx.SmoothingMode = SmoothingMode.AntiAlias; 
      gfx.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      gfx.PixelOffsetMode = PixelOffsetMode.HighQuality; 
      gfx.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel); 
      // Dispose to free up resources 
      image.Dispose(); 
      //bmp.Dispose(); 
      gfx.Dispose(); 

      return bmp; 
     } 
     catch (Exception ex) 
     { 
      //MessageBox.Show(ex.Message); 
      return null; 
     } 

我也试过这样:

Bitmap temp = (Bitmap)System.Drawing.Image.FromFile(img); 
     Bitmap bmap = (Bitmap)temp.Clone(); 
     if (xPosition + width > temp.Width) 
      width = temp.Width - xPosition; 
     if (yPosition + height > temp.Height) 
      height = temp.Height - yPosition; 
     Rectangle rect = new Rectangle(xPosition, yPosition, width, height); 
     temp = (Bitmap)bmap.Clone(rect, bmap.PixelFormat); 

我写了这一点,以上下文流:

Bitmap bm = Helper.CropImage(@"MyFileLocation", 0, 0, 300, 223); 
     context.Response.ContentType = "image/jpg"; 
     bm.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); 
     bm.Dispose(); 

有趣的是,当我尝试一个tiff图像,并更改上下文类型时,我收到一个通用的GDI +错误。从研究来看,这看起来像一个寻求问题,但不知道如何解决它。

+0

你如何发布到PDF? – Groo 2009-06-23 08:58:32

回答

1

使用PDF时,您必须记住您正在查看打印分辨率而不是屏幕分辨率。

600 x 600像素的图像将在1280 x 1024分辨率的显示器上占据大约一半的屏幕宽度。

但是,如果打印输出是200 dpi,它将占用3英寸,但如果它设置为300 dpi,它将只占用2英寸。

我对PDF格式不太了解,无法说明您需要做什么才能使其发挥作用,但我的猜测是,您需要通过输出的dpi来从纸张上的物理尺寸恢复工作得到的图像的像素大小:

pixel width = physical width * dpi 
0

关于GDI +错误的问题,尽量保存到一个MemoryStream第一,然后复制该到Response.OutputStream。如果Tiff像PNG一样,那么这个流确实需要可以搜索。

相关问题