나는 기능이 있습니다변환 모든
// 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로 변환 할 수 있습니까?
편집 : 자세한 정보는 여기에 대한 사진입니다.
여기 좀 보아라 : http://stackoverflow.com/questions/1108607/out-of-memory-exception-on-system-drawing-image-fromfile –
그 스택 추적은 당신이 만든 코드와 맞지 않는다. 제공된 ... 그럴듯한'OriginalImage' 코드에 – Shai
@ RaphaëlAlthaus yea 내가 전에 확인한 .. 사진과 함께 아무 것도 GDI +가이 문제를 일으켰습니다. –