把一个bitmap在内存中的数据块 搬到另一个bitmap中

这些纯粹学习代码没啥实际用处

bitmap快速复制数据的方法,比setPixel快

static void  void Main(string[] args)
{
    //截屏代码
    Rectangle rec = Screen.PrimaryScreen.Bounds;
    Bitmap img = new Bitmap(rec.Width, rec.Height);
    Graphics gph = Graphics.FromImage(img);
    gph.CopyFromScreen(new Point(0, 0), new Point(0, 0), rec.Size);

    //将bitmap锁定到系统内存中
    System.Drawing.Imaging.BitmapData bmpData = img.LockBits(rec, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
    // 获取起始行的地址.
    IntPtr ptr = bmpData.Scan0;
    // 定义一个数组用来保存bitmap的数据.
    int bytes = bmpData.Stride * img.Height;
    byte[] rgbValues = new byte[bytes];
    // 把rgb值拷贝到数组中
    System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
    img.UnlockBits(bmpData);

    //将数据拷到另一个bitmap中
    Bitmap img2 = new Bitmap(rec.Width, rec.Height);
    Graphics gph2 = Graphics.FromImage(img2);
    System.Drawing.Imaging.BitmapData bmpData2 = img2.LockBits(rec, System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
    IntPtr ptr2 = bmpData2.Scan0;
    System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr2, bytes);
    img2.UnlockBits(bmpData2);
    img2.Save("asd.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);                                                                                                      
}                                                                                                                                                                           

转载于:https://www.cnblogs.com/assassinx/archive/2011/03/09/1978530.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值