zoukankan      html  css  js  c++  java
  • (转)打印相关_C#图片处理Bitmap位图缩放和剪裁

       原文地址:http://blog.sina.com.cn/s/blog_6427a6b50101el9d.html

    在GDI+中,缩放和剪裁可以看作同一个操作,无非就是原始区域的选择不同罢了。

            ///
            /// Resize图片
            ///
            /// 原始Bitmap
            /// 新的宽度
            /// 新的高度
            /// 保留着,暂时未用
            /// 处理以后的图片
            public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH, int Mode)
            {
                try
                {
                    Bitmap b = new Bitmap(newW, newH);
                    Graphics g = Graphics.FromImage(b);

                    // 插值算法的质量
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                    g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                    g.Dispose();

                    return b;
                }
                catch
                {
                    return null;
                }
            }

    // ===============================

             ///
            /// 剪裁 -- 用GDI+
            ///
            /// 原始Bitmap
            /// 开始坐标X
            /// 开始坐标Y
            /// 宽度
            /// 高度
            /// 剪裁后的Bitmap
            public static Bitmap KiCut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
            {
                if (b == null)
                {
                    return null;
                }

                int w = b.Width;
                int h = b.Height;

                if (StartX >= w || StartY >= h)
                {
                    return null;
                }

                if (StartX + iWidth > w)
                {
                    iWidth = w - StartX;
                }

                if (StartY + iHeight > h)
                {
                    iHeight = h - StartY;
                }

                try
                {
                    Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);

                    Graphics g = Graphics.FromImage(bmpOut);
                    g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
                    g.Dispose();

                    return bmpOut;
                }
                catch
                {
                    return null;
                }
            }

    注意到区别g.DrawImage中第二个new Rectangle。目标其实都是new Rectangle(0, 0, iWidth, iHeight),缩放算法把整个原始图都往目标区域里塞new Rectangle(0, 0, bmp.Width, bmp.Height),而剪裁只是把原始区域上等宽等高的那个区域new Rectangle(StartX, StartY, iWidth, iHeight)1:1的塞到目标区域里

         打印相关内容:

          图片裁剪:http://blog.csdn.net/redder_xu/article/details/6760589

          图片像素相关:http://www.cnblogs.com/yjmyzz/archive/2012/01/09/2316892.html

  • 相关阅读:
    Let Us Adore 让我们来敬拜祂 中文歌词
    Way Maker 开路者 歌词
    Great Things 伟大的事 歌词
    永活盼望 Living Hope 歌词
    TP 控制器和模型里面order 写法不同
    服务器安全记录
    NOTIC: [8] Trying to get property of non-object
    Declaration of AdminControllerGameController::delete() should be compatible with。。
    vi编辑器操作 快捷键
    Camtasia如何录制小文件视频
  • 原文地址:https://www.cnblogs.com/hhhh2010/p/6246390.html
Copyright © 2011-2022 走看看