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

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

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

下面是今天的方法:

(1)Bitmap.MakeTransparent 方法:使此 Bitmap的默认透明颜色透明。

private void MakeTransparent_Example1(PaintEventArgs e)
{

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

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

    // Make the default transparent color transparent for myBitmap.
    myBitmap.MakeTransparent();

    // Draw the transparent bitmap to the screen.
    e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0,
        myBitmap.Width, myBitmap.Height);
}

private void MakeTransparent_Example2(PaintEventArgs e)
{

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

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

    // Get the color of a background pixel.
    Color backColor = myBitmap.GetPixel(1, 1);

    // Make backColor transparent for myBitmap.
    myBitmap.MakeTransparent(backColor);

    // Draw the transparent bitmap to the screen.
    e.Graphics.DrawImage(
        myBitmap, myBitmap.Width, 0, myBitmap.Width, myBitmap.Height);
}

(2)Bitmap.SetPixel(Int32, Int32, Color) 方法:设置此 Bitmap中指定像素的颜色。

private void SetPixel_Example(PaintEventArgs e)
{

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

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

    // Set each pixel in myBitmap to black.
    for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++)
    {
        for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++)
        {
            myBitmap.SetPixel(Xcount, Ycount, Color.Black);
        }
    }

    // Draw myBitmap to the screen again.
    e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0,
        myBitmap.Width, myBitmap.Height);
}

(3)Bitmap.SetResolution(Single, Single) 方法:设置此 Bitmap 的分辨率。

(4)Bitmap.UnlockBits(BitmapData) 方法:从系统内存解锁此 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);
    }

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

http://www.lryc.cn/news/426854.html

相关文章:

  • Vue是如何实现nextTick的?
  • rabbitmq镜像集群搭建
  • 《c++并发编程实战》 笔记
  • 57qi5rW35LqRZUhS pc.mob SQL注入漏洞复现
  • 微信小程序--27(自定义组件4)
  • Linux | Linux进程万字全解:内核原理、进程状态转换、优先级调度策略与环境变量
  • VBA技术资料MF184:图片导入Word添加说明文字设置格式
  • 在函数设计中应用单一职责原则:函数分解与职责分离
  • 多线程锁机制面试
  • 《SQL 中计算地理坐标两点间距离的魔法》
  • 微服务可用性设计
  • 【扒代码】dave readme文档翻译
  • c语言---文件
  • Windows系统下Go安装与使用
  • day24-测试之接口测试基础
  • TSN 交换机
  • 针对thinkphp站点的漏洞挖掘和经验分享
  • MySQL数据库入门,pycharm连接数据库—详细讲解
  • .bat文件快速运行vue项目
  • 数据结构(邓俊辉)学习笔记】优先级队列 07——堆排序
  • npm install pnpm -g 报错的解决方法
  • 集师知识付费小程序开发
  • 前端开发提效工具——用户自定义代码片段
  • docker容器安全加固参考建议——筑梦之路
  • 基于 Appium 的 App 爬取实战
  • nvm与node安装
  • 【电子通识】什么是MSL湿敏等级
  • 【ARM 芯片 安全与攻击 5.4 -- Meltdown 攻击与防御介绍】
  • Django 后端架构开发:分页器到中间件开发
  • 亲测解决The client socket has failed to connect to