zoukankan      html  css  js  c++  java
  • 截取图片的某个部分(C#源代码)

    方法一(get/set pixel)

    核心语句:

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

    代码
    1 /// <summary>
    2 /// get a certain rectangle part of a known graphic
    3 /// </summary>
    4 /// <param name="bitmapPathAndName">path and name of the source graphic</param>
    5 /// <param name="width">width of the part graphic</param>
    6 /// <param name="height">height of the part graphic</param>
    7 /// <param name="offsetX">the width offset in the source graphic</param>
    8 /// <param name="offsetY">the height offset in the source graphic</param>
    9 /// <returns>wanted graphic</returns>
    10   public Bitmap GetPartOfImage(string bitmapPathAndName, int width, int height,int offsetX,int offsetY)
    11 {
    12 Bitmap sourceBitmap = new Bitmap(bitmapPathAndName);
    13 Bitmap resultBitmap = new Bitmap(width, height);
    14 for (int x = 0; x < width; x++)
    15 {
    16 for (int y = 0; y < height; y++)
    17 {
    18 if (offsetX + x < sourceBitmap.Size.Width & offsetY + y < sourceBitmap.Size.Height)
    19 {
    20 resultBitmap.SetPixel(x, y, sourceBitmap.GetPixel(offsetX + x, offsetY+y));
    21 }
    22 }
    23 }
    24 return resultBitmap;
    25 }

    该方法速度较慢

    方法二(graphics.drawimage)

    核心代码:

    Graphics g = Graphics.FromImage(resultBitmap)

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

    代码
    1 /// <summary>
    2 /// get a certain rectangle part of a known graphic
    3 /// </summary>
    4 /// <param name="bitmapPathAndName">path and name of the source graphic</param>
    5 /// <param name="width">width of the part graphic</param>
    6 /// <param name="height">height of the part graphic</param>
    7 /// <param name="offsetX">the width offset in the source graphic</param>
    8 /// <param name="offsetY">the height offset in the source graphic</param>
    9 /// <returns>wanted graphic</returns>
    10   public Bitmap GetPartOfImage(string bitmapPathAndName, int width, int height, int offsetX, int offsetY)
    11 {
    12 Bitmap sourceBitmap = new Bitmap(bitmapPathAndName);
    13 Bitmap resultBitmap = new Bitmap(width, height);
    14 using (Graphics g = Graphics.FromImage(resultBitmap))
    15 {
    16 Rectangle resultRectangle = new Rectangle(0, 0, Width, height);
    17 Rectangle sourceRectangle = new Rectangle(0+offsetX, 0+offsetY, Width, height);
    18 g.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
    19 }
    20 return resultBitmap;
    21 }

    速度较快,可完全鄙视掉方法一

  • 相关阅读:
    (转)KMP算法实现。超级赞!见过的最容易理解的
    《越狱》观后感
    【Coursera】Security Introduction -Summary
    【Coursera】Security Introduction -Ninth Week(2)
    【TCP/IP详解 卷一:协议】第十八章 TCP连接 的建立与终止 (2)其余内容
    【Coursera】Security Introduction -Eighth Week(2)
    【TCP/IP详解 卷一:协议】第十八章 TCP连接 的建立与终止 (1)三次握手,四次挥手
    【Coursera】Security Introduction -Eighth Week(1)
    【TCP/IP详解 卷一:协议】第十七章 TCP:传输控制协议
    【Coursera】Seventh Week
  • 原文地址:https://www.cnblogs.com/snigoal/p/1118209.html
Copyright © 2011-2022 走看看