BITMAP

导入以下两个包:
     System.Drawing;
     System.Drawing.Imaging;
 
 
建产对象:
     Bitmap bm = new Bitmap("c:/1.bmp");
 
缩放:
     Bitmap bm1 = new Bitmap(bm,width,height);
 
格式转换:
     bm.save("c:/1.jpg",ImageFromat.Jpeg);
     bm1.Save("c:/1.gif", ImageFormat.Gif);
 
剪切一个区域:
     //剪切大小
     int cutwidth;  
     int cutheight;
     Graphics g;
     //以大小为剪切大小,像素格式为32位RGB创建一个位图对像
     Bitmap bm1 = new Bitmap(width,height,PixelFormat.Format32bppRgb) ;
     //定义一个区域
     Rectangle rg = new Rectangle(0,0,cutwidth,cutheight);
     //要绘制到的位图
     g = Graphics.FromImage(bm1);
     //将bm内rg所指定的区域绘制到bm1
     g.DrawImage(bm,rg)
============================================
C#Bitmap代替另一个Bitmap的某部分 Bitmap bm = new Bitmap(宽度, 高度);// 新建一个 Bitmap 位图 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm); // 根据新建的 Bitmap 位图,创建画布 g.Clear(System.Drawing.Color.Black);// 使用黑色重置画布 g.DrawImage(源位图, ......); // 绘制“源位图”,后面有若干参数控制大小、坐标等等功能。
/// <summary>
         /// 任意角度旋转
         /// </summary>
         /// <param name="bmp">原始图Bitmap</param>
         /// <param name="angle">旋转角度</param>
         /// <param name="bkColor">背景色</param>
         /// <returns>输出Bitmap</returns>
         public static Bitmap KiRotate(Bitmap bmp, float angle, Color bkColor)
         {
             int w = bmp.Width + 2;
             int h = bmp.Height + 2;
 
              PixelFormat pf;
 
             if (bkColor == Color.Transparent)
             {
                  pf = PixelFormat.Format32bppArgb;
              }
             else
             {
                  pf = bmp.PixelFormat;
              }
 
              Bitmap tmp = new Bitmap(w, h, pf);
              Graphics g = Graphics.FromImage(tmp);
              g.Clear(bkColor);
              g.DrawImageUnscaled(bmp, 1, 1);
              g.Dispose();
 
              GraphicsPath path = new GraphicsPath();
              path.AddRectangle(new RectangleF(0f, 0f, w, h));
              Matrix mtrx = new Matrix();
              mtrx.Rotate(angle);
              RectangleF rct = path.GetBounds(mtrx);
 
              Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf);
              g = Graphics.FromImage(dst);
              g.Clear(bkColor);
              g.TranslateTransform(-rct.X, -rct.Y);
              g.RotateTransform(angle);
              g.InterpolationMode = InterpolationMode.HighQualityBilinear;
              g.DrawImageUnscaled(tmp, 0, 0);
              g.Dispose();
 
              tmp.Dispose();
 
             return dst;
          }

上面的是查到的资料用来方便记忆,下面的是自己写的有关自己做的项目中的一部分,是关于图片识别的程序部分代码

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            

            textBox1.Text = "";
            Bitmap s;
            Bitmap img = ZoomAuto(textBox2.Text, 1000, 1000, out s) as Bitmap;
            pictureBox1.Image = s;
            Tesseract ocr = new Tesseract();
            
            ocr.SetVariable("tessedit_char_whitelist", "0123456789.");
            ocr.Init(AppDomain.CurrentDomain.BaseDirectory + "Language", "eng", false);
            List<Word> result = ocr.DoOCR(img, Rectangle.Empty);
            foreach (Word item in result)
            {
                textBox1.Text += item.Text;
            }
            img.Dispose();
            
        }
        /// <summary>
        /// 等比例缩放图片
        /// </summary>
        /// <param name="fromFile"></param>
        /// <param name="targetWidth"></param>
        /// <param name="targetHeight"></param>
        /// <param name="img"></param>
        /// <returns></returns>
        public static Image ZoomAuto(string fromFile, System.Double targetWidth, System.Double targetHeight, out Bitmap img)
        {
            //原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)
            System.Drawing.Image initImage = Image.FromFile(fromFile);
            img = new Bitmap(initImage);
            double newWidth = initImage.Width;
            double newHeight = initImage.Height;

            //宽大于高或宽等于高(横图或正方)
            if (initImage.Width >= initImage.Height)
            {
                newWidth = targetWidth;
                newHeight = initImage.Height * (targetWidth / initImage.Width);
            }
            //高大于宽(竖图)
            else
            {
                newHeight = targetHeight;
                newWidth = initImage.Width * (targetHeight / initImage.Height);
            }

            //生成新图
            //新建一个bmp图片
            System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);
            //新建一个画板
            System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);

            //设置质量
            newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //置背景色
            newG.Clear(Color.White);
            //画图
            newG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);


            newG.Dispose();
            initImage.Dispose();
            //保存缩略图
            return newImage;
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }


    }

 

posted @ 2013-09-03 15:00  思想启蒙家  阅读(481)  评论(0编辑  收藏  举报