当前位置: 首页 > news >正文

C#:Bitmap类使用方法—第3讲

大家好,今天接着上篇文章讲。

下面是今天的内容:

(1)Bitmap.GetHicon 方法:返回图标的句柄。

下面是其使用的例子:

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = CharSet.Auto)]
extern static bool DestroyIcon(IntPtr handle);

private void GetHicon_Example(PaintEventArgs e)
{

    // Create a Bitmap object from an image file.
    Bitmap myBitmap = new Bitmap(@"c:\FakePhoto.jpg");

    // Draw myBitmap to the screen.
    e.Graphics.DrawImage(myBitmap, 0, 0);

    // Get an Hicon for myBitmap.
    IntPtr Hicon = myBitmap.GetHicon();

    // Create a new icon from the handle. 
    Icon newIcon = Icon.FromHandle(Hicon);

    // Set the form Icon attribute to the new icon.
    this.Icon = newIcon;

    // You can now destroy the icon, since the form creates
    // its own copy of the icon accessible through the Form.Icon property.
    DestroyIcon(newIcon.Handle);
}

(2)Bitmap.GetPixel(Int32, Int32) 方法:获取此 Bitmap中指定像素的颜色。

下面是其使用的例子:

private void GetPixel_Example(PaintEventArgs e)
{

    // Create a Bitmap object from an image file.
    Bitmap myBitmap = new Bitmap("Grapes.jpg");

    // Get the color of a pixel within myBitmap.
    Color pixelColor = myBitmap.GetPixel(50, 50);

    // Fill a rectangle with pixelColor.
    SolidBrush pixelBrush = new SolidBrush(pixelColor);
    e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100);
}

(3)Bitmap.LockBits 方法:将 Bitmap 锁定到系统内存中。

下面是其使用的例子:

private void LockUnlockBitsExample(PaintEventArgs e)
    {

        // Create a new bitmap.
        Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");

        // Lock the bitmap's bits.  
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        System.Drawing.Imaging.BitmapData bmpData =
            bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
            bmp.PixelFormat);

        // Get the address of the first line.
        IntPtr ptr = bmpData.Scan0;

        // Declare an array to hold the bytes of the bitmap.
        int bytes  = Math.Abs(bmpData.Stride) * bmp.Height;
        byte[] rgbValues = new byte[bytes];

        // Copy the RGB values into the array.
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

        // Set every third value to 255. A 24bpp bitmap will look red.  
        for (int counter = 2; counter < rgbValues.Length; counter += 3)
            rgbValues[counter] = 255;

        // Copy the RGB values back to the bitmap
        System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

        // Unlock the bits.
        bmp.UnlockBits(bmpData);

        // Draw the modified image.
        e.Graphics.DrawImage(bmp, 0, 150);
    }

今天要介绍的就是这么多,我们下篇文章再见。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 探索ACPL-302J光耦合器的多功能性
  • 区块链浪潮:Web3时代的数字经济新格局
  • 【开端】 如何判断手机号码属于哪个国家(手机号判断正则)汇总
  • Java二十三种设计模式-访问者模式(21/23)
  • 高性能计算应用优化之运行参数优化
  • 探索地理空间分析的新世界:Geopandas的魔力
  • 前端Array.reduce()函数延申用法
  • OpenGL-ES 学习(8) ---- FBO
  • 一款好看的WordPress REST API 主题
  • 又双叒叕JavaScript 新增了 7 个方法!
  • 无人机技术的最新进展及未来趋势
  • 智能指针,然并卵
  • 精致潮人们,抠搜在电商平台
  • 前端常见**MS题 [3]
  • Android常见控件(一)
  • Hibernate【inverse和cascade属性】知识要点
  • iOS动画编程-View动画[ 1 ] 基础View动画
  • Java 11 发布计划来了,已确定 3个 新特性!!
  • Javascript设计模式学习之Observer(观察者)模式
  • Mybatis初体验
  • Python打包系统简单入门
  • SSH 免密登录
  • Terraform入门 - 1. 安装Terraform
  • Vim Clutch | 面向脚踏板编程……
  • 从地狱到天堂,Node 回调向 async/await 转变
  • 回流、重绘及其优化
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 经典排序算法及其 Java 实现
  • 老板让我十分钟上手nx-admin
  • 聊聊directory traversal attack
  • 人脸识别最新开发经验demo
  • 通过获取异步加载JS文件进度实现一个canvas环形loading图
  • 网络应用优化——时延与带宽
  • 为视图添加丝滑的水波纹
  • 详解NodeJs流之一
  • 终端用户监控:真实用户监控还是模拟监控?
  • 追踪解析 FutureTask 源码
  • 最近的计划
  • ​configparser --- 配置文件解析器​
  • (11)MATLAB PCA+SVM 人脸识别
  • (php伪随机数生成)[GWCTF 2019]枯燥的抽奖
  • (void) (_x == _y)的作用
  • (二)pulsar安装在独立的docker中,python测试
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (九)One-Wire总线-DS18B20
  • (十)【Jmeter】线程(Threads(Users))之jp@gc - Stepping Thread Group (deprecated)
  • (一)C语言之入门:使用Visual Studio Community 2022运行hello world
  • (转)setTimeout 和 setInterval 的区别
  • .apk 成为历史!
  • .NET : 在VS2008中计算代码度量值
  • .NET大文件上传知识整理
  • .net实现客户区延伸至至非客户区
  • .NET实现之(自动更新)
  • [ web基础篇 ] Burp Suite 爆破 Basic 认证密码
  • [2023年]-hadoop面试真题(一)