zoukankan      html  css  js  c++  java
  • [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变量
  • 相关阅读:
    python爬取图片
    IDEA创建SpringBoot项目报错,不能连接https://start.spring.io/
    ES模块化的理解
    Web标准(网站标准)理解
    Mongodb安装
    Linux Ntp时间同步服务
    SentinelResource 注解
    Sentinel的流量控制
    Sentinel简介
    nacos安装
  • 原文地址:https://www.cnblogs.com/hcbin/p/1693951.html
Copyright © 2011-2022 走看看