I have 10MB+ Tiff files having 50 pages, but when I am going to rotate all these pages with degree =90 that time I am getting exception
System.IO.FileFormatException: 'The image decoder cannot decode the image. The image might be corrupted.'
here IEnumerable<int> pageNumbers
is the number of pages and degree = angle to rotate the image.
trasformedBitmap.EndInit(); this line is throwing exception.
Type : System.IO.FileFormatException
Message : The image decoder cannot decode the image. The image might be corrupted.
HResult : 0x80131537
Source : PresentationCore
at System.Windows.Media.PixelFormat.GetPixelFormat(SafeMILHandle bitmapSource)
Inner Exception: COMException: The image is unrecognized. (Exception from HRESULT: 0x88982F60)
StackTrace " at System.Windows.Media.PixelFormat.GetPixelFormat(SafeMILHandle bitmapSource)
at System.Windows.Media.Imaging.BitmapSource.CreateCachedBitmap(BitmapFrame frame, BitmapSourceSafeMILHandle wicSource, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, BitmapPalette palette)
at System.Windows.Media.Imaging.TransformedBitmap.FinalizeCreation()
at System.Windows.Media.Imaging.TransformedBitmap.EndInit()
TargetSite: {System.Windows.Media.PixelFormat GetPixelFormat(System.Windows.Media.SafeMILHandle)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
code snippets:
private void RotateImagePages(IEnumerable<int> pageNumbers, Degrees degree)
{
// create the encoder
BitmapEncoder encoder = BitmapEncoder.Create(Decoder.CodecInfo.ContainerFormat);
// copy the destination frames
foreach (BitmapFrame frame in Decoder.Frames)
encoder.Frames.Add(frame);
double angleOfRotation = (double)degree;
foreach (var pageNumber in pageNumbers)
{
BitmapFrame oldFrame = encoder.Frames[pageNumber - 1];
// Create the TransformedBitmap to use as the Image source.
TransformedBitmap trasformedBitmap = new TransformedBitmap();
// Properties must be set between BeginInit and EndInit calls.
trasformedBitmap.BeginInit();
trasformedBitmap.Source = oldFrame;
RotateTransform rotateTransform = new RotateTransform(angleOfRotation);
trasformedBitmap.Transform = rotateTransform;
trasformedBitmap.EndInit();
encoder.Frames.RemoveAt(pageNumber - 1);
encoder.Frames.Insert(pageNumber - 1, BitmapFrame.Create(trasformedBitmap));
Save(encoder);
}
}
private void Save(BitmapEncoder encoder)
{
// save to a temporary stream
string tempFileName = Path.GetTempFileName();
try
{
using (FileStream temporaryStream = new FileStream(tempFileName, FileMode.Open))
{
if (encoder.Frames.Count > 0)
encoder.Save(temporaryStream);
// write back out to permanent stream
if (Stream.CanWrite && Stream.CanSeek)
CopyStream(temporaryStream, Stream);
else
throw new UnauthorizedAccessException();
}
}
finally
{
// Delete the temporary file
File.Delete(tempFileName);
}
}
protected static void CopyStream(Stream input, Stream output)
{
input.Seek(0, SeekOrigin.Begin);
output.Seek(0, SeekOrigin.Begin);
output.SetLength(0);
byte[] buffer = new byte[4096];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
output.Write(buffer, 0, read);
output.Seek(0, SeekOrigin.Begin);
}
User contributions licensed under CC BY-SA 3.0