zoukankan      html  css  js  c++  java
  • 截取部分BMP图像

    方法1:

    resultBitmap.SetPixel(x, y, sourceBitmap.GetPixel(offsetX + x, offsetY+y))

     

     /// <summary>   

     /// get a certain rectangle part of a known graphic   

     /// </summary>   

     /// <param name="bitmapPathAndName">path and name of the source graphic</param>   

    /// <param name="width">width of the part graphic</param>   

    /// <param name="height">height of the part graphic</param>   

    /// <param name="offsetX">the width offset in the source graphic</param>   

    /// <param name="offsetY">the height offset in the source graphic</param>   

    /// <returns>wanted graphic</returns>   

    public Bitmap GetPartOfImage(string bitmapPathAndName, int width, int height,int offsetX,int offsetY)   

    {   

       Bitmap sourceBitmap = new Bitmap(bitmapPathAndName);   

        Bitmap resultBitmap = new Bitmap(width, height);   

        for (int x = 0; x < width; x++)   

        {   

            for (int y = 0; y < height; y++)   

            {   

                if (offsetX + x < sourceBitmap.Size.Width & offsetY + y < sourceBitmap.Size.Height)    

                {   

                    resultBitmap.SetPixel(x, y, sourceBitmap.GetPixel(offsetX + x, offsetY+y));   

                }   

            }   

        }   

        return resultBitmap;   

    }    

          

    方法2:

     

    Graphics g = Graphics.FromImage(resultBitmap)

    g.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel)

    /// <summary>   

    /// get a certain rectangle part of a known graphic   

    /// </summary>   

    /// <param name="bitmapPathAndName">path and name of the source graphic</param>   

    /// <param name="width">width of the part graphic</param>   

    /// <param name="height">height of the part graphic</param>   

    /// <param name="offsetX">the width offset in the source graphic</param>   

    /// <param name="offsetY">the height offset in the source graphic</param>   

    /// <returns>wanted graphic</returns>   

    public Bitmap GetPartOfImage(string bitmapPathAndName, int width, int height, int offsetX, int offsetY)   

    {   

        Bitmap sourceBitmap = new Bitmap(bitmapPathAndName);   

        Bitmap resultBitmap = new Bitmap(width, height);   

        using (Graphics g = Graphics.FromImage(resultBitmap))   

        {   

            Rectangle resultRectangle = new Rectangle(0, 0, Width, height);   

            Rectangle sourceRectangle = new Rectangle(0+offsetX, 0+offsetY, Width, height);   

            g.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);   

        }   

        return resultBitmap;   

    
  • 相关阅读:
    关于NB-IoT,没有比这篇更通俗易懂的啦!
    使用SQL Server 2014 In-Memory 内存数据库时需要注意的地方
    SQL Server Profiler:使用方法和指标说明
    SQL Server 维护计划实现数据库备份(策略实战)
    SQL Server 连接问题圣经-命名管道
    如何查看 exec sp_execute 10 XXX, XXXX的RPC事件 内容
    Mac下Qt连接MySQL 驱动问题
    {Nodejs} 错误 HPE_INVALID_CONSTANT 解决
    {Nodejs} request URL 中文乱码
    Centos 5.5 安装 Git 以及配置
  • 原文地址:https://www.cnblogs.com/mane/p/2005585.html
Copyright © 2011-2022 走看看