[C#(WinForm)] - 疑难杂症

1.GDI+ 中发生一般性错误。(从数据库读取图片,没有修改过就写回数据库,而引发的)

参考:http://www.cnblogs.com/wudingfeng/archive/2008/07/24/1250564.html

byte [] pic = null ; // 保存的图片
if (PictureBox1.Image != null )
{
// 解决"GDI+ 中发生一般性错误。"的关键
// -----------------------------------------------------------------------------------
Image img = PictureBox1.Image; // 读取PictureBox的图片
// 如果文件格式为空就设置为Bmp类型
System.Drawing.Imaging.ImageFormat rawFormat = null ;
rawFormat
= (img.RawFormat == null ) ? System.Drawing.Imaging.ImageFormat.Bmp : img.RawFormat;

Bitmap bmp1
= new Bitmap(img); // 创建一个bitmap类型的bmp变量来读取文件。
Bitmap bmp2 = new Bitmap(img.Width, img.Height); // 新建第二个bitmap类型的bmp2变量
Graphics draw = Graphics.FromImage((Image)bmp2);
draw.DrawImage((Image)bmp1,
0 , 0 );
PictureBox1.Image
= (Image)bmp2;
draw.Dispose();
bmp1.Dispose();
// bmp2.Dispose(); // 暂时不能释放
// -----------------------------------------------------------------------------------

System.IO.MemoryStream imgMS
= new System.IO.MemoryStream();
PictureBox1.Image.Save(imgMS, rawFormat);
byte [] imgByte = imgMS.ToArray();
if (imgMS.Length > 64 * 1024 )
{
PictureBox1.Image
= null ; // 清除图片
if (imgMS != null ) { imgMS.Dispose(); } // 释放资源
System.Windows.Forms.MessageBox.Show( " 请选择容量小于等于64KB的图片! " , " 警告 " ,
System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
}
imgMS.Read(imgByte,
0 , Convert.ToInt32(imgMS.Length));
pic
= imgByte;
imgMS.Close();
if (imgMS != null ) { imgMS.Dispose(); } // 释放资源
}

/* ************* 省略存储过程 ************* */
mysqlDA.UpdateCommand.Parameters.AddWithValue(
" @Photo " , pic);

 

 

 

 

2.无法从带有索引像素格式的图像创建 Graphics 对象。

参考:http://hi.baidu.com/1987raymond/blog/item/0d2e86a151d969834710649b.html

(水印的例子参考:http://dong.hongjun888.blog.163.com/blog/static/2081208420098172655812/)

依照上面代码[1.GDI+ 中发生一般性错误。(从数据库读取图片,没有修改过就写回数据库,而引发的)]

只要在new Bitmap(img.Width, img.Height,像素格式);中去除像素格式就可以了(本人暂时只遇到这种情况,和找到类似的解决方法)

Bitmap bmp2 = new Bitmap(img.Width, img.Height); // 新建第二个bitmap类型的bmp2变量

转载于:https://www.cnblogs.com/hcbin/archive/2010/03/24/1693951.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值