2012-06-11 2 views
2

나는 기능이 있습니다변환 모든

// Convert To JPG 
    // 
    public string AlltoJPG(FileInfo foo) 
    { 
     // Get file extension 
     string fileExtension = foo.Extension; 

     // Get file name without extenstion 
     string fileName = foo.Name.Replace(foo.Extension, string.Empty) + ".jpg"; 

     /*------------------------------------------------------------------------*/ 

     /// <Check for PNG File Format> 
     if (fileExtension == ".png" || fileExtension == ".PNG") 
     { 
      System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName); 

      // Assumes img is the PNG you are converting 
      using (Bitmap b = new Bitmap(img.Width, img.Height)) 
      { 
       using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b)) 
       { 
        g.Clear(System.Drawing.Color.White); 
        g.DrawImageUnscaled(img, 0, 0); 
       } 

       // Save the image as a JPG 
       b.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); 
      } 

     } 

     /*------------------------------------------------------------------------*/ 

     /// <Check for GIF File Format> 
     if (fileExtension == ".gif" || fileExtension == ".GIF") 
     { 
      System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName); 

      // Construct a bitmap from the image resource. 
      Bitmap bmp1 = new Bitmap(img.Width, img.Height); 

      // Save the image as a JPG 
      bmp1.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); 
     } 

     /*------------------------------------------------------------------------*/ 

     /// <Check for BMP File Format> 
     if (fileExtension == ".bmp" || fileExtension == ".BMP") 
     { 
      System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName); 

      // Construct a bitmap from the image resource. 
      Bitmap bmp1 = new Bitmap(img.Width, img.Height); 

      // Save the image as a JPG 
      bmp1.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); 
     } 

     /*------------------------------------------------------------------------*/ 

     /// <Check for TIFF File Format> 
     if (fileExtension == ".tiff" || fileExtension == ".TIFF") 
     { 
      System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName); 

      // Construct a bitmap from the image resource. 
      Bitmap bmp1 = new Bitmap(img.Width, img.Height); 

      // Save the image as a JPG 
      bmp1.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); 
     } 

     /*------------------------------------------------------------------------*/ 
     fileName = foo.DirectoryName + "\\" + fileName; 
     return fileName; 
    } 

메신저 JPG에 BMP-PNG-GIF-TIFF 파일 formattes을 변환하려고하지만, 평소와 같이 GDI +가 제공 :

System.OutOfMemoryException was unhandled 
    Message=Bellek yetersiz. 
    Source=System.Drawing 
    StackTrace: 
     konum: System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) 
     konum: System.Drawing.Image.FromFile(String filename) 

그래서 어떻게 피할 수 있고 적어도 PNG 및 BMP 파일을 JPG로 변환 할 수 있습니까?

편집 : 자세한 정보는 여기에 대한 사진입니다.

enter image description here

+1

여기 좀 보아라 : http://stackoverflow.com/questions/1108607/out-of-memory-exception-on-system-drawing-image-fromfile –

+4

그 스택 추적은 당신이 만든 코드와 맞지 않는다. 제공된 ... 그럴듯한'OriginalImage' 코드에 – Shai

+0

@ RaphaëlAlthaus yea 내가 전에 확인한 .. 사진과 함께 아무 것도 GDI +가이 문제를 일으켰습니다. –

답변

3

문제점을 발견했습니다!

How to load Transparent PNG to Bitmap and ignore alpha channel


public static void SetAlpha(this Bitmap bmp, byte alpha) 
{ 
    if(bmp == null) throw new ArgumentNullException("bmp"); 

    var data = bmp.LockBits(
     new Rectangle(0, 0, bmp.Width, bmp.Height), 
     System.Drawing.Imaging.ImageLockMode.ReadWrite, 
     System.Drawing.Imaging.PixelFormat.Format32bppArgb); 

    var line = data.Scan0; 
    var eof = line + data.Height * data.Stride; 
    while(line != eof) 
    { 
     var pixelAlpha = line + 3; 
     var eol = pixelAlpha + data.Width * 4; 
     while(pixelAlpha != eol) 
     { 
      System.Runtime.InteropServices.Marshal.WriteByte(
       pixelAlpha, alpha); 
      pixelAlpha += 4; 
     } 
     line += data.Stride; 
    } 
    bmp.UnlockBits(data); 
} 

사용법 :

var pngImage = new Bitmap("filename.png"); 
pngImage.SetAlpha(255); 

그것은 최대에 .. PNG의 알파 채널에 의해

감사 기인했다, 링크는 아래에있는 내 문제를 해결 알파를 설정 한 후 그것은 올바르게 작동하기 시작했습니다.

0

있는 GIF, PNG 및 TIFF 코드 블록이없는 using 조항과 GDI 참조를 누출하고 있습니다.

관련 문제