码迷,mamicode.com
首页 > 其他好文 > 详细

dada

时间:2015-01-28 23:58:15      阅读:395      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void LockUnlockBitsExample(PaintEventArgs e)
        {
            //Create a new bitmap
            Bitmap bmp = new Bitmap("house.jpg");

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

            //Get the address of the fist 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
            Marshal.Copy(ptr, rgbValues, 0, bytes);

            //Set every third value to 255. A 24bpp bitmap will look red
            //bitmap图像是BGR的方式存储的。
            for (int counter = 1; counter < rgbValues.Length; counter += 3)
            {
                rgbValues[counter] = 255;
            }

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

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

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

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            LockUnlockBitsExample(e);
        }
    }
}

 

dada

标签:

原文地址:http://www.cnblogs.com/stemon/p/4257450.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!