G
Guest1993
Guest
I am trying to set compression type from LZW to Ccitt3. I researched online and got this far:
using (Bitmap bitmap = new Bitmap(sourcePath))
{
Encoder encoder = Encoder.Compression;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(encoder, (long)EncoderValue.CompressionCCITT4);
myEncoderParameters.Param[0] = myEncoderParameter;
ImageCodecInfo myImageCodecInfo;
myImageCodecInfo = GetEncoderInfo("image/tiff");
Brush brush = new SolidBrush(Color.Black);
Font font = new Font("Arial", 50, FontStyle.Italic, GraphicsUnit.Pixel);
Bitmap tempBitmap = new Bitmap(bitmap.Width, bitmap.Height);
Graphics tempGraphics = Graphics.FromImage(tempBitmap);
SizeF textSize = tempGraphics.MeasureString(text, font);
tempBitmap = new Bitmap(bitmap.Width, bitmap.Height + (int)textSize.Height + 10);
tempBitmap.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(tempBitmap))
{
graphics.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height + 100);
graphics.DrawImage(bitmap,0,0 , bitmap.Width, bitmap.Height );
Point position = new Point(bitmap.Width - ((int)textSize.Width + 200), bitmap.Height+5);
//Point position = new Point((int)((bitmap.Width - textSize.Width) / 2), bitmap.Height + 5);
graphics.DrawString((text), font, brush, position);
tempBitmap.Save(destinationPathh, ImageFormat.Tiff, myImageCodecInfo, myEncoderParameters);
}
}
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 null;
}
In this line I want to apply Imageformat and encode the image and save it in one line.
I want to avoid :
tempBitmap.Save(destinationPathh, ImageFormat.Tiff);
Newname.Save( tempBitmap, myImageCodecInfo, myEncoderParameters);
Issue: can't convert bitmap to io.stream.
Continue reading...
using (Bitmap bitmap = new Bitmap(sourcePath))
{
Encoder encoder = Encoder.Compression;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(encoder, (long)EncoderValue.CompressionCCITT4);
myEncoderParameters.Param[0] = myEncoderParameter;
ImageCodecInfo myImageCodecInfo;
myImageCodecInfo = GetEncoderInfo("image/tiff");
Brush brush = new SolidBrush(Color.Black);
Font font = new Font("Arial", 50, FontStyle.Italic, GraphicsUnit.Pixel);
Bitmap tempBitmap = new Bitmap(bitmap.Width, bitmap.Height);
Graphics tempGraphics = Graphics.FromImage(tempBitmap);
SizeF textSize = tempGraphics.MeasureString(text, font);
tempBitmap = new Bitmap(bitmap.Width, bitmap.Height + (int)textSize.Height + 10);
tempBitmap.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(tempBitmap))
{
graphics.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height + 100);
graphics.DrawImage(bitmap,0,0 , bitmap.Width, bitmap.Height );
Point position = new Point(bitmap.Width - ((int)textSize.Width + 200), bitmap.Height+5);
//Point position = new Point((int)((bitmap.Width - textSize.Width) / 2), bitmap.Height + 5);
graphics.DrawString((text), font, brush, position);
tempBitmap.Save(destinationPathh, ImageFormat.Tiff, myImageCodecInfo, myEncoderParameters);
}
}
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 null;
}
In this line I want to apply Imageformat and encode the image and save it in one line.
I want to avoid :
tempBitmap.Save(destinationPathh, ImageFormat.Tiff);
Newname.Save( tempBitmap, myImageCodecInfo, myEncoderParameters);
Issue: can't convert bitmap to io.stream.
Continue reading...