Invalid Argument exception?
-
Hi all, I have try to convert a pdf to tiff file.My below the code snippet is working fine when i open a tiff file and convert some other format..But it didn't work when i open the pdf file,it will be thrown error like "Parameter is not valid" from below the code at bold line "Image image = Image.FromStream(fromImageStream, false,true);" please help me what i did wrong.
private void button4_Click(object sender, System.EventArgs e) { FileStream fstPersons = new FileStream(@"c:\\toto.tiff", FileMode.Create,FileAccess.Write); BinaryWriter wrtPersons = new BinaryWriter(fstPersons); FileStream fStream = new FileStream(@"c:\\Sample.pdf", FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(fStream); FileInfo info = new FileInfo(@"c:\\sample.pdf"); byte[] fromImage = null; fromImage = reader.ReadBytes((int)info.Length); reader.Close(); fStream.Close(); // Read the image from the byte variable into a bitmap variable MemoryStream fromImageStream = new MemoryStream(); fromImageStream.Write(fromImage, 0, fromImage.Length); **Image image = Image.FromStream(fromImageStream, false,true);** Bitmap bitmap = (Bitmap)image; // Instantiate the encoder EncoderParameters encoderParams = new EncoderParameters(); encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 50L); ImageCodecInfo codecInfo = GetEncoderInfo("image/tiff"); MemoryStream newImage = new MemoryStream(); // Convert the image to the new format bitmap.Save(newImage, codecInfo, encoderParams); // Read the new image into a byte variable byte[] data = newImage.ToArray(); wrtPersons.Write(data); fstPersons.Close(); wrtPersons.Close(); } private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; encoders = ImageCodecInfo.GetImageEncoders(); for(j = 0; j < encoders.Length; ++j) { if(encoders[j].MimeType == mimeType) return encoders[j]; } return nul
-
Hi all, I have try to convert a pdf to tiff file.My below the code snippet is working fine when i open a tiff file and convert some other format..But it didn't work when i open the pdf file,it will be thrown error like "Parameter is not valid" from below the code at bold line "Image image = Image.FromStream(fromImageStream, false,true);" please help me what i did wrong.
private void button4_Click(object sender, System.EventArgs e) { FileStream fstPersons = new FileStream(@"c:\\toto.tiff", FileMode.Create,FileAccess.Write); BinaryWriter wrtPersons = new BinaryWriter(fstPersons); FileStream fStream = new FileStream(@"c:\\Sample.pdf", FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(fStream); FileInfo info = new FileInfo(@"c:\\sample.pdf"); byte[] fromImage = null; fromImage = reader.ReadBytes((int)info.Length); reader.Close(); fStream.Close(); // Read the image from the byte variable into a bitmap variable MemoryStream fromImageStream = new MemoryStream(); fromImageStream.Write(fromImage, 0, fromImage.Length); **Image image = Image.FromStream(fromImageStream, false,true);** Bitmap bitmap = (Bitmap)image; // Instantiate the encoder EncoderParameters encoderParams = new EncoderParameters(); encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 50L); ImageCodecInfo codecInfo = GetEncoderInfo("image/tiff"); MemoryStream newImage = new MemoryStream(); // Convert the image to the new format bitmap.Save(newImage, codecInfo, encoderParams); // Read the new image into a byte variable byte[] data = newImage.ToArray(); wrtPersons.Write(data); fstPersons.Close(); wrtPersons.Close(); } private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; encoders = ImageCodecInfo.GetImageEncoders(); for(j = 0; j < encoders.Length; ++j) { if(encoders[j].MimeType == mimeType) return encoders[j]; } return nul
Stream operations are very much like good old file operations: if you write a file, then want to read what has been written, you must either close and reopen, OR somehow "rewind" the file. Look for an appropriate stream method (or property) to do this... :)
Luc Pattyn [My Articles] [Forum Guidelines]