如何改变位图的dpi和大小,我的代码总是异常报出(GDI+)

ladofwind 2010-04-06 03:08:17
//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


...全文
180 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
junwenwang 2010-06-14
  • 打赏
  • 举报
回复
而且没有delete bmpSource
junwenwang 2010-06-14
  • 打赏
  • 举报
回复
保存的参数加上引用符
ladofwind 2010-04-08
  • 打赏
  • 举报
回复
还没搞定
ladofwind 2010-04-07
  • 打赏
  • 举报
回复
没人会吗, 其实就是想把一个bmp的图,改变下dpi和width ,height 然后用G4压缩一下成tif图片,总报错
我将带领大家来系统学习Windows的窗口编程,包括消息、窗口、GDI绘图、游戏开发等。本课程比较基础,非常适合初学者入门,读者可以边学习边实践。具体的章节目录和课程内容如下所示:---------------------------------------------Windows游戏编程系列之1:GUI界面编程及游戏入门实战1、Windows创建第一个窗口 WinMain入口函数 5进行Windows编程的调试手法 6窗口从哪里来? 7窗口编程的步骤 7窗口编程需要的主要结构 8窗口编程需要的主要API 92、Windows的窗口过程与消息机制 如何留住窗口? 121)Windows的消息与消息循环 142)消息处理函数与常用消息 17)Windows的窗口过程函数 19 3、GDI编程之设备上下文 1)GDI的通用编程框架 222)GDI的绘图步骤 253)GDI获取设备句柄 254、GDI编程之绘制几何图形 画点、线 28颜色COLORREF 29矩形 29画圆、饼图、弦图 305、GDI编程之自定义画笔画刷画笔简介 32画刷简介 33画笔案例 33画刷案例 346、GDI编程之绘制文字 DrawText函数 35TextOut 函数 (wingdi.h) 36CreateFont函数 37绘制文本案例 377、GDI编程之绘制位图 位图简介 381)在资源中添加位图资源 392)从资源中加载位图: LoadBitmap 393)创建一个与当前DC相匹配的DC(内存DC) 394)将bitmap放入匹配的DC中:SelectObject 405)成像(1:1 比例 ) 406)取出位图 407)释放位图 418)释放匹配的DC 41绘制位图案例 41   8、Windows鼠标键盘消息 一、键盘消息 421、键盘消息 422、消息参数: 423、消息的使用: 424、键盘消息的案例代码 43二、鼠标消息 441、基本鼠标消息 442、双击消息 443、滚轮消息 454、不响应双击消息 45 9、Windows定时器消息 定时器消息介绍 47创建定时器 47关闭定时器 47定时器消息案例代码 4810、GDI游戏之跳舞动画 11、GDI游戏之走路动画 12、GDI贪吃蛇游戏实战  

110,524

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧