C#栅格金字塔(最邻近法)

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;


namespace RasterPyramid
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
            this.pictureBox1.MouseClick += new MouseEventHandler(pictureBox1_MouseClick);
        }


        void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            this.pictureBox1.Focus();
            //throw new NotImplementedException();
        }


        private void Form1_Load(object sender, EventArgs e)
        {


            Bitmap img = (Bitmap)Bitmap.FromFile("D:\\dvim3.TIF");
            this.pictureBox1.Image = img;
            this.pictureBox1.Show();


            this.pictureBox1.Image = pyramid_nearest(img);
            
        }


        void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta>0)
            {
                float w = this.pictureBox1.Width*1.2f;
                float h = this.pictureBox1.Height*1.2f;
                this.pictureBox1.Size = Size.Ceiling(new SizeF(w, h));
            }
            else
            {
                float w = this.pictureBox1.Width * 0.8f;
                float h = this.pictureBox1.Height * 0.8f;
                this.pictureBox1.Size = Size.Ceiling(new SizeF(w, h));
            }
        }


        /// <summary>
        /// 栅格金字塔抽层  耗时间 不可取
        /// </summary>
        /// <param name="preBitmap">原栅格</param>
        /// <returns>抽取后的栅格</returns>
        private Bitmap pyramid_nearest(Bitmap preBitmap)
        {
            int height = preBitmap.Height;
            int width = preBitmap.Width;
            Color c = new Color();
            Bitmap buildedMap = new Bitmap(width/2,height/2);


            int m = 0, n = 0;
            for (int i = 1; i < height; i = i + 3)
            {
                for (int j = 1; j < width; j = j + 3)
                {
                    c = preBitmap.GetPixel(j, i);
                    buildedMap.SetPixel(n, m, c);


                    n += 1;
                }
                m += 1;
                n = 0;
            }


            return buildedMap;
        }
        /// <summary>
        /// 栅格金字塔抽层  内存法
        /// </summary>
        /// <param name="preBitmap">原栅格</param>
        /// <returns>抽取后的栅格</returns>
        private Bitmap pyramid_nearest_2(Bitmap preBitmap)
        {
            int wide = preBitmap.Width;
            int height = preBitmap.Height;
            Rectangle rect = new Rectangle(0, 0, wide, height);
            //将Bitmap锁定到系统内存中,获得BitmapData
            BitmapData srcBmData = preBitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            //创建Bitmap
            Bitmap dstBitmap = new Bitmap(wide/3,height/3);
            Rectangle dstrest = new Rectangle(0,0,wide / 3, height / 3);
            BitmapData dstBmData = dstBitmap.LockBits(dstrest, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            System.IntPtr srcPtr = srcBmData.Scan0;
            System.IntPtr dstPtr = dstBmData.Scan0;
            int src_bytes = srcBmData.Stride * height;
            byte[] srcValues = new byte[src_bytes];
            int dst_bytes = dstBmData.Stride * height/3;
            byte[] dstValues = new byte[dst_bytes];
            //复制GRB信息到byte数组
            System.Runtime.InteropServices.Marshal.Copy(srcPtr, srcValues, 0, src_bytes);
            System.Runtime.InteropServices.Marshal.Copy(dstPtr, dstValues, 0, dst_bytes);


            int m = 0, n = 0;
            for (int i = 1; i < height; i=i+3)
            {
                for (int j = 1; j < wide;j=j+3)
                {
                    int k = 3 * j;
                    dstValues[m * dstBmData.Stride + 3*n + 1] = srcValues[i * srcBmData.Stride + k + 1];
                    dstValues[m * dstBmData.Stride + 3 * n + 2] = srcValues[i * srcBmData.Stride + k + 2];
                    dstValues[m * dstBmData.Stride + 3 * n] = srcValues[i * srcBmData.Stride + k];


                    n += 1;
                }
                m += 1;
                n = 0;
            }
            System.Runtime.InteropServices.Marshal.Copy(dstValues, 0, dstPtr, dst_bytes);
            //解锁位图
            preBitmap.UnlockBits(srcBmData);
            dstBitmap.UnlockBits(dstBmData);
            return dstBitmap;
        }
        /// <summary>
        /// 栅格金字塔抽层 unsafe
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        private Bitmap pyramid_nearest_3(Bitmap preBitmap)
        {
         
            int wide = preBitmap.Width;
            int height = preBitmap.Height;


            Bitmap dstBitmap = new Bitmap(wide/3,height/3);


            Rectangle rect = new Rectangle(0, 0, wide, height);
            Rectangle dstrect = new Rectangle(0, 0, wide/3, height/3);
            //将Bitmap锁定到系统内存中,获得BitmapData
            BitmapData srcBmData = preBitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            BitmapData dstBmData = dstBitmap.LockBits(dstrect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);


            unsafe
            {
                byte* ptr = (byte*)(srcBmData.Scan0);
                byte* newbyte = (byte*)(dstBmData.Scan0);
                int m = 0;
                int n = 0;
                for (int i = 1; i < height; i = i + 3)
                {
                    for (int j = 1; j < wide; j = j + 3)
                    {
                        int k = 3*j;
    ;
                        newbyte[m * dstBmData.Stride + 3*n + 1] = ptr[i * srcBmData.Stride + k + 1];
                        newbyte[m * dstBmData.Stride + 3*n + 2] = ptr[i * srcBmData.Stride + k + 2];
                        newbyte[m * dstBmData.Stride + 3*n] = ptr[i * srcBmData.Stride + k];


                        n += 1;
                       // ptr += k;
                    }
                    m += 1;
                    n = 0;
                }
                preBitmap.UnlockBits(srcBmData);
                dstBitmap.UnlockBits(dstBmData);
                return dstBitmap;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值