C# PictureBox对显示的图像实现 像素的修改方式

本文介绍使用C#在PictureBox中修改图像像素的方法,包括使用函数生成图像和通过内存方式修改像素。提供了完整的示例代码,展示了如何通过改变RGB值来调整图像的颜色。

C# PictureBox对显示的图像实现 像素的修改方式

介绍了两个方式去操作pictureBox

  1. 使用函数修改
  2. 使用内存的方式去修改

函数的方式适合初始化图片,通过函数生成一个基础的图像,才能在内存中有文件信息。从而再去操作其图像

1. 使用函数修改

        private void Pixel_Click()
        {

            System.Drawing.Bitmap curBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb) ;
            for (int i = 0; i < pictureBox1.Width; i++)
            {
                for(int j = 0; j < pictureBox1.Height; j++)
                {
                    // newImg.SetPixel(i, j,Color.Black);//直接给黑色
                    //通过RGB的方式给颜色
                    curBitmap.SetPixel(i, j, Color.FromArgb(255, 255, 255));
                }
            }
            pictureBox1.Image = curBitmap;
        }

2. 内存的方式修改

        //使用memory的方式去显示图片,刷新小像素的时候比较好,刷新6K*4K 的时候还是可以感受到明显的延迟
        //通过地址的方式读取数据,只能更改图片,不能创建图片。
        private void memory_1Chick(Bitmap bmp)
        {
            if(bmp != null)
            {
                //转换格式
                BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                IntPtr ptr = bmpData.Scan0; //获得图像的地址

                //定义一个数组 ,得到长度
                int bytes = bmp.Width * bmp.Height * 3;//24 bit bmp picture
                //int bytes = 200 * 200 * 3;
                
                byte[] rgbValues = new byte[bytes]; 

                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

                double colorTemp = 0;
                for(int i = 0; i < rgbValues.Length; i += 3)
                {
                    //colorTemp = rgbValues[i + 2] * 0.299 + rgbValues[i + 1] * 0.587 + rgbValues[i] * 0x114;
                    //RGB = color
                    rgbValues[i] = rgbValues[i+1] = rgbValues[i + 2] = (byte)(10 * number11); 

                }

                System.Runtime.InteropServices.Marshal.Copy(rgbValues,0,ptr,bytes);
                bmp.UnlockBits(bmpData);  
                //Invalidate();   //跟新像素,没有起到效果

            }

        }

3. form1的完整代码参考

实现了打开应用的时候对picturebox初始化,初始化完成之后按按键,每次都会有颜色的变化,因为rgb三个都是相同值,所以显示的是不同颜色的灰色。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Pixel_Click();
        }
        byte number11 = 0;



        private void button1_Click(object sender, EventArgs e)
        {

            number11 ++;
            label1.Text = number11.ToString();
            string path = "111.JPG";

            

            Image img = this.pictureBox1.Image;

            Debug.Write(pictureBox1.Width + "width\n");
            Debug.Write(pictureBox1.Height+ "Height\n");

            Bitmap bitmap = new Bitmap(img);
            memory_1Chick(bitmap);
            pictureBox1.Image = bitmap;
        }



        private void Pixel_Click()
        {

            System.Drawing.Bitmap curBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb) ;
            for (int i = 0; i < pictureBox1.Width; i++)
            {
                for(int j = 0; j < pictureBox1.Height; j++)
                {
                    // newImg.SetPixel(i, j,Color.Black);
                    curBitmap.SetPixel(i, j, Color.FromArgb(255, 255, 255));
                }
            }
            pictureBox1.Image = curBitmap;
        }



        //使用memory的方式去显示图片,刷新小像素的时候比较好,刷新6K*4K 的时候还是可以感受到明显的延迟
        //通过地址的方式读取数据,只能更改图片,不能创建图片。
        private void memory_1Chick(Bitmap bmp)
        {
            if(bmp != null)
            {
                //转换格式
                BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                IntPtr ptr = bmpData.Scan0; //获得图像的地址

                //定义一个数组 ,得到长度
                int bytes = bmp.Width * bmp.Height * 3;//24 bit bmp picture
                //int bytes = 200 * 200 * 3;
                
                byte[] rgbValues = new byte[bytes]; 

                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

                double colorTemp = 0;
                for(int i = 0; i < rgbValues.Length; i += 3)
                {
                    //colorTemp = rgbValues[i + 2] * 0.299 + rgbValues[i + 1] * 0.587 + rgbValues[i] * 0x114;
                    //RGB = color
                    rgbValues[i] = rgbValues[i+1] = rgbValues[i + 2] = (byte)(10 * number11); 

                }

                System.Runtime.InteropServices.Marshal.Copy(rgbValues,0,ptr,bytes);
                bmp.UnlockBits(bmpData);  
                //Invalidate();   //跟新像素,没有起到效果

            }

        }


        //功能:输入图片路径,读成byte数据
        //参数是图片的路径
        public byte[] GetPictureData(string imagePath)
        {
            FileStream fs = new FileStream(imagePath, FileMode.Open);
            byte[] byteData = new byte[fs.Length];
            fs.Read(byteData, 0, byteData.Length);//读内存
            fs.Close();
            return byteData;
        }


        //写函数
        //功能:输入byte 数据,输出image数据
        //
        //
        //问题:输入byte数据的格式

        public System.Drawing.Image ReturnPhoto(byte[] streamByte)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
            return img;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值