Dear all
i m creating an album of images in Tiff format
here is sample code
Behind button1_click
private void button1_Click(object sender, EventArgs e)
{
for (int i = 1; i < 3;i++ )
{
Pen LinePen = new Pen(Color.Black);
Bitmap img1 = new Bitmap(2481, 3492);
img1.SetResolution(300f, 300f);
Graphics printer = Graphics.FromImage(img1);
printer.FillRectangle(Brushes.White, 0, 0, img1.Width, img1.Height);
Bitmap pic = new Bitmap("c:\\regimage.jpg");
printer.DrawImage(pic, 90, 90, 200, 200);
printer.DrawImage(pic, 90, 710, 200, 200);
printer.DrawImage(pic, 90, 1400, 200, 200);
printer.DrawImage(pic, 90, 2000, 200, 200);
printer.DrawImage(pic, 90, 2700, 200, 200);
printer.DrawImage(pic, 90, 3200, 200, 200);
printer.DrawRectangle(LinePen, 850, 650, 400, 100);
this.SaveImage(img1, i.ToString() , "1");
printer.Dispose();
img1.Dispose();
}
//// image codec info
private static ImageCodecInfo getEncoderInfo(string mimeType)
{
ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
for (int index = 0; index < imageEncoders.Length; ++index)
{
if (imageEncoders[index].MimeType == mimeType)
return imageEncoders[index];
}
return (ImageCodecInfo)null;
}
//// save image
private void SaveImage(Bitmap b, string page, string book_no)
{
EncoderParameter bitParam = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 24L);
EncoderParameter compressionParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 4L);
EncoderParameter Quality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality , 100L);
EncoderParameters myparams = new EncoderParameters(1);
myparams.Param[0] = bitParam;
ImageCodecInfo mycodec = getEncoderInfo("image/tiff");
Bitmap pages;
pages = new Bitmap(b);
pages.SetResolution(300f, 300f);
MemoryStream memoryStream = new MemoryStream();
pages.Save(memoryStream, mycodec, myparams);
if (Convert.ToInt32(page) == 1)
{
tiff = Image.FromStream(memoryStream);
EncoderParameters encoderParams1 = new EncoderParameters(2);
EncoderParameter parameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 4L);
encoderParams1.Param[0] = parameter;
parameter = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, 18);
encoderParams1.Param[1] = parameter;
tiff.Save("e:\\image1.tiff", mycodec, encoderParams1);
}
else
{
Image temp = Image.FromStream(memoryStream);
EncoderParameters EncoderParams2 = new EncoderParameters(2);
EncoderParameter SaveEncodeParam = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, 23);
EncoderParameter CompressionEncodeParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 4L);
EncoderParams2.Param[0] = CompressionEncodeParam;
EncoderParams2.Param[1] = SaveEncodeParam;
tiff.SaveAdd(temp, EncoderParams2); ///////// GDI+ Generic Error on SaveAdd method
temp.Dispose();
}
try
{
b.Dispose();
memoryStream.Dispose();
bitParam.Dispose();
//compressionParam.Dispose();
myparams.Dispose();
}
catch (Exception)
{
}
}
///// Here is the error
tiff.SaveAdd(temp, EncoderParams2); ///////// GDI+ Generic Error on SaveAdd method
tiff.SaveAdd is not adding second frame and giving GDI+ Generic Error
How to solve this . what encoder parameters to be used
can someone help
Regards,
i m creating an album of images in Tiff format
here is sample code
Behind button1_click
private void button1_Click(object sender, EventArgs e)
{
for (int i = 1; i < 3;i++ )
{
Pen LinePen = new Pen(Color.Black);
Bitmap img1 = new Bitmap(2481, 3492);
img1.SetResolution(300f, 300f);
Graphics printer = Graphics.FromImage(img1);
printer.FillRectangle(Brushes.White, 0, 0, img1.Width, img1.Height);
Bitmap pic = new Bitmap("c:\\regimage.jpg");
printer.DrawImage(pic, 90, 90, 200, 200);
printer.DrawImage(pic, 90, 710, 200, 200);
printer.DrawImage(pic, 90, 1400, 200, 200);
printer.DrawImage(pic, 90, 2000, 200, 200);
printer.DrawImage(pic, 90, 2700, 200, 200);
printer.DrawImage(pic, 90, 3200, 200, 200);
printer.DrawRectangle(LinePen, 850, 650, 400, 100);
this.SaveImage(img1, i.ToString() , "1");
printer.Dispose();
img1.Dispose();
}
//// image codec info
private static ImageCodecInfo getEncoderInfo(string mimeType)
{
ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
for (int index = 0; index < imageEncoders.Length; ++index)
{
if (imageEncoders[index].MimeType == mimeType)
return imageEncoders[index];
}
return (ImageCodecInfo)null;
}
//// save image
private void SaveImage(Bitmap b, string page, string book_no)
{
EncoderParameter bitParam = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, 24L);
EncoderParameter compressionParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 4L);
EncoderParameter Quality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality , 100L);
EncoderParameters myparams = new EncoderParameters(1);
myparams.Param[0] = bitParam;
ImageCodecInfo mycodec = getEncoderInfo("image/tiff");
Bitmap pages;
pages = new Bitmap(b);
pages.SetResolution(300f, 300f);
MemoryStream memoryStream = new MemoryStream();
pages.Save(memoryStream, mycodec, myparams);
if (Convert.ToInt32(page) == 1)
{
tiff = Image.FromStream(memoryStream);
EncoderParameters encoderParams1 = new EncoderParameters(2);
EncoderParameter parameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 4L);
encoderParams1.Param[0] = parameter;
parameter = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, 18);
encoderParams1.Param[1] = parameter;
tiff.Save("e:\\image1.tiff", mycodec, encoderParams1);
}
else
{
Image temp = Image.FromStream(memoryStream);
EncoderParameters EncoderParams2 = new EncoderParameters(2);
EncoderParameter SaveEncodeParam = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, 23);
EncoderParameter CompressionEncodeParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 4L);
EncoderParams2.Param[0] = CompressionEncodeParam;
EncoderParams2.Param[1] = SaveEncodeParam;
tiff.SaveAdd(temp, EncoderParams2); ///////// GDI+ Generic Error on SaveAdd method
temp.Dispose();
}
try
{
b.Dispose();
memoryStream.Dispose();
bitParam.Dispose();
//compressionParam.Dispose();
myparams.Dispose();
}
catch (Exception)
{
}
}
///// Here is the error
tiff.SaveAdd(temp, EncoderParams2); ///////// GDI+ Generic Error on SaveAdd method
tiff.SaveAdd is not adding second frame and giving GDI+ Generic Error
How to solve this . what encoder parameters to be used
can someone help
Regards,