如何改变位图的dpi和大小,我的代码总是异常报出(GDI+)
//add by ladofwind,change bitmap size and resolution
Bitmap bmpSource = new Bitmap(((Bitmap)this.images[0]), 1653, 2338);
int iSourceWidth = bmpSource.Width;
int iSourceHeight = bmpSource.Height;
float fSourceHorResolution = bmpSource.HorizontalResolution;
float fSourceVerResolution = bmpSource.VerticalResolution;
//dpi reset
float fTargetHorResolution = 200;
float fTargetVerResolution = 200;
Bitmap bmpTarget = new Bitmap(1653, 2338);
Graphics g = Graphics.FromImage(bmpTarget);
g.DrawImage(bmpSource, 0, 0, iSourceWidth, iSourceHeight);
bmpTarget.SetResolution(fTargetHorResolution, fTargetVerResolution);
ImageCodecInfo info = GetEncoderInfo("image/tiff");
Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = null;
if (this.pbMain.Image.PixelFormat == PixelFormat.Format1bppIndexed) //G4 compression
{
ep = new EncoderParameters(3);
ep.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
ep.Param[1] = new EncoderParameter(Encoder.Quality, 0L);//
ep.Param[2] = new EncoderParameter(Encoder.ColorDepth, 1L); // black/white
}
bmpTarget.Save(filename,info,ep); // exception thrown