基本信息
源码名称:C# 位图转换 示例源码
源码大小:0.36M
文件格式:.zip
开发语言:C#
更新时间:2018-11-17
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍


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

namespace 位图转换
{
    public partial class Form1 : Form
    {



        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        Bitmap curBitmap;

        Bitmap bit;

        Stopwatch st;

        



        private void button1_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();


            if (op.ShowDialog() == DialogResult.OK)
            {

                curBitmap = (Bitmap)Bitmap.FromFile(op.FileName);

                pictureBox1.Image = curBitmap;

            }

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if (curBitmap != null)
            {

                st = new Stopwatch();

                st.Start();



                Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);



                System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);

                IntPtr ptr = bmpData.Scan0;

                int bytes = bmpData.Stride * bmpData.Height;

                byte[] rgbValues = new byte[bytes];

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

                Rectangle rect2 = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);

                bit = new Bitmap(curBitmap.Width, curBitmap.Height, PixelFormat.Format8bppIndexed);

                System.Drawing.Imaging.BitmapData bmpData2 = bit.LockBits(rect2, System.Drawing.Imaging.ImageLockMode.ReadWrite, bit.PixelFormat);

                IntPtr ptr2 = bmpData2.Scan0;

                int bytes2 = bmpData2.Stride * bmpData2.Height;

                byte[] rgbValues2 = new byte[bytes2];

                System.Runtime.InteropServices.Marshal.Copy(ptr2, rgbValues2, 0, bytes2);


                double colorTemp = 0;

                for (int i = 0; i < bmpData.Height; i  )
                {

                    for (int j = 0; j < bmpData.Width * 3; j  = 3)
                    {

                        colorTemp = rgbValues[i * bmpData.Stride   j   2] * 0.299   rgbValues[i * bmpData.Stride   j   1] * 0.578   rgbValues[i * bmpData.Stride   j] * 0.114;

                        rgbValues2[i * bmpData2.Stride   j / 3] = (byte)colorTemp;

                    }

                }



                System.Runtime.InteropServices.Marshal.Copy(rgbValues2, 0, ptr2, bytes2);

                curBitmap.UnlockBits(bmpData);

                ColorPalette tempPalette;

                {

                    using (Bitmap tempBmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
                    {

                        tempPalette = tempBmp.Palette;

                    }

                    for (int i = 0; i < 256; i  )
                    {

                        tempPalette.Entries[i] = Color.FromArgb(i, i, i);

                    }

                    bit.Palette = tempPalette;

                }

                bit.UnlockBits(bmpData2);

                st.Stop();

                pictureBox1.Image = bit;

                textBox1.Text = st.ElapsedMilliseconds.ToString();
                string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "saveimage");// @"C:\Users\asus\Desktop\c#\位图转换\位图转换\bin\Debug\saveimage\";

                bit.Save(path   DateTime.Now.ToString("yyyyMMddHHmmssff")   ".bmp");


            }

        }    

    }
}