截取图片的某个部分(C#源代码) css截取图片一部分

方法一(get/setpixel)

核心语句:

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

C#代码
  1. ///<summary>
  2. ///getacertainrectanglepartofaknowngraphic
  3. ///</summary>
  4. ///<paramname="bitmapPathAndName">pathandnameofthesourcegraphic</param>
  5. ///<paramname="width">widthofthepartgraphic</param>
  6. ///<paramname="height">heightofthepartgraphic</param>
  7. ///<paramname="offsetX">thewidthoffsetinthesourcegraphic</param>
  8. ///<paramname="offsetY">theheightoffsetinthesourcegraphic</param>
  9. ///<returns>wantedgraphic</returns>
  10. publicBitmapGetPartOfImage(stringbitmapPathAndName,intwidth,intheight,intoffsetX,intoffsetY)
  11. {
  12. BitmapsourceBitmap=newBitmap(bitmapPathAndName);
  13. BitmapresultBitmap=newBitmap(width,height);
  14. for(intx=0;x<width;x++)
  15. {
  16. for(inty=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. returnresultBitmap;
  25. }
        /// <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;        } 

该方法速度较慢

方法二(graphics.drawimage)

核心代码:

Graphics g =Graphics.FromImage(resultBitmap)

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

C#代码
  1. ///<summary>
  2. ///getacertainrectanglepartofaknowngraphic
  3. ///</summary>
  4. ///<paramname="bitmapPathAndName">pathandnameofthesourcegraphic</param>
  5. ///<paramname="width">widthofthepartgraphic</param>
  6. ///<paramname="height">heightofthepartgraphic</param>
  7. ///<paramname="offsetX">thewidthoffsetinthesourcegraphic</param>
  8. ///<paramname="offsetY">theheightoffsetinthesourcegraphic</param>
  9. ///<returns>wantedgraphic</returns>
  10. publicBitmapGetPartOfImage(stringbitmapPathAndName,intwidth,intheight,intoffsetX,intoffsetY)
  11. {
  12. BitmapsourceBitmap=newBitmap(bitmapPathAndName);
  13. BitmapresultBitmap=newBitmap(width,height);
  14. using(Graphicsg=Graphics.FromImage(resultBitmap))
  15. {
  16. RectangleresultRectangle=newRectangle(0,0,Width,height);
  17. RectanglesourceRectangle=newRectangle(0+offsetX,0+offsetY,Width,height);
  18. g.DrawImage(sourceBitmap,resultRectangle,sourceRectangle,GraphicsUnit.Pixel);
  19. }
  20. returnresultBitmap;
  21. }
        /// <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;        }

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

using(Graphicsg=this.CreateGraphics())
{
截取图片的某个部分(C#源代码) css截取图片一部分
g.CopyFromScreen(0,0,100,100,newSize(100,100));
}

原帖地址:http://wostyh.javaeye.com/blog/471765

  

爱华网本文地址 » http://www.413yy.cn/a/25101011/57270.html

更多阅读

C++11中的lambda表达式 c lambda表达式 linq

C++11添加了一项名为lambda表达式的新功能,通过这项功能可以编写内嵌的匿名函数,而不必编写独立函数和函数对象,使得代码更容易理解。lambda表达式的语法如下所示:[capture_block](parameters) exceptions_specification-> return_type

关于C语言中的restrict关键字 c语言关键字

一、新的C语言:一切都源于FORTRAN(restrict的历史)http://www.chinaunix.net/ 作者:phoneix 发表于:2007-06-17 09:18:45此篇文章摘取于即将登载于《Dr.Dobb's 软件研发》第三期(2003年10月)的《The New C:一切源于FORTRAN》,文章主要是介

C ++ 中对象或其对象指针的赋值 c语言指针赋值

C++中对象或其对象指针的赋值C++中成员函数的动态绑定:C++中要实现函数的动态绑定,必须在其基类中将函数声明为virtual且在子类中对函数加以实现。然后用一个基类指针指向某一个子类对象,这样才会在函数调用时实现动态绑定。在C++中,只

模拟c++中的strstr函数 c语言中strstr函数

/************************************************************************//* 例子:请写出一个函数来模拟c++中的strstr函数:该函数的返回值是主传中字符子串的位置以后的所有字符,请不要使用任何c程序已有的函数函数名: strstr函

声明:《截取图片的某个部分(C#源代码) css截取图片一部分》为网友相见两相厌分享!如侵犯到您的合法权益请联系我们删除