1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
String inFilePath = @"D:\inFile.tif";
String outFilePath = @"D:\outFile.tif";
// Ouverture de l'image à redimensionner
FileInfo inFile = new FileInfo(inFilePath);
// Stockage de l'image dans un flux
byte[] readByteInFile = new byte[inFile.Length];
(inFile.OpenRead()).Read(readByteInFile, 0, Convert.ToInt32(inFile.Length));
Stream tmpStream = new MemoryStream(readByteInFile, 0, eadByteInFile.Length);
Image img = Bitmap.FromStream(tmpStream);
//*** Redimensionnement de l'image
Bitmap bitmap;
int w, h;
int destinationDpi = 200;
w = Int32.Parse((img.Width * (destinationDpi / Convert.ToInt32(img.HorizontalResolution))).ToString());
w =Convert.ToInt32 (img.Width * (destinationDpi / img.HorizontalResolution ));
h = Convert.ToInt32 (img.Height * (destinationDpi / img.VerticalResolution ));
bitmap = new Bitmap(w,h);
bitmap.SetResolution(destinationDpi,destinationDpi);
Graphics g = Graphics.FromImage((Image)bitmap);
g.DrawImage(img, 0, 0, w, h);
img.Dispose();
img = null;
//*** Fin redimensionnement de l'image
// Définition du format de l'image en TIFF
ImageCodecInfo tiffCodecInfo = GetEncoderInfo("image/tiff");
EncoderParameters myEncoderParameters = new EncoderParameters(2);
// Compression de l'image en CCITT group4
myEncoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
// Encodage de l'image en bitonal
myEncoderParameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 1L);
// sauvegarde de l'image
bitmap.Save(outFilePath, tiffCodecInfo, myEncoderParameters); // <= L'ERREUR ARRIVE ICI
tmpStream.Close();
g.Dispose();
bitmap.Dispose(); |
Partager