C#版水波效果 源程序

一个很不错的c#实现的图片特效,相应源程序如下:

FASTBitmap.cs

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Drawing;

using System.Drawing.Imaging;

 

namespace WaveEffects

{

    public unsafe class FastBitmap {

 

        public struct PixelData {

            public byte blue;

            public byte green;

            public byte red;

            public byte alpha;

        }

 

        Bitmap Subject;

        int SubjectWidth;

        BitmapData bitmapData = null;

        Byte* pBase = null;

        bool isLocked = false;

        int _bits = 0;

 

        public FastBitmap(Bitmap SubjectBitmap, int bits) {

            this.Subject = SubjectBitmap;

            _bits = bits;

            try {

                //LockBits();

            } catch (Exception ex) {

                throw ex;

            }            

        }

 

        public void Release() {

            try {

                UnlockBits();

            } catch (Exception ex) {

                throw ex;

            }

        }

 

        public Bitmap Bitmap {

            get {

                return Subject;

            }

        }

 

        public void SetPixel(int X, int Y, Color Colour) {

            try {

                PixelData* p = PixelAt(X, Y);

                p->red = Colour.R;

                p->green = Colour.G;

                p->blue = Colour.B;

            } catch (AccessViolationException ave) {

                throw (ave);

            } catch (Exception ex) {

                throw ex;

            }

        }

 

        public Color GetPixel(int X, int Y) {

            try {

                PixelData* p = PixelAt(X, Y);

                return Color.FromArgb((int)p->red, (int)p->green, (int)p->blue);

            } catch (AccessViolationException ave) {

                throw (ave);

            } catch (Exception ex) {

                throw ex;

            }            

        }

 

        public int Width() { return Subject.Width; }

        public int Height() { return Subject.Height; }

        public bool  IsLocked() { return isLocked; }

        public BitmapData Data() { return bitmapData; }

 

        public void LockBits() {

            if (isLocked) return;

            try

            {                

                GraphicsUnit unit = GraphicsUnit.Pixel;

                RectangleF boundsF = Subject.GetBounds(ref unit);

                Rectangle bounds = new Rectangle((int)boundsF.X,

                    (int)boundsF.Y,

                    (int)boundsF.Width,

                    (int)boundsF.Height);

 

                SubjectWidth = (int)boundsF.Width * sizeof(PixelData);

                if (SubjectWidth % _bits != 0)

                {

                    SubjectWidth = _bits * (SubjectWidth / _bits + 1);

                }

                if (_bits == 3)

                    bitmapData = Subject.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

                else 

                    bitmapData = Subject.LockBits(bounds, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);

                pBase = (Byte*)bitmapData.Scan0.ToPointer();

            }

            finally

            {

                isLocked = true ;

            }

        }

 

        private PixelData* PixelAt(int x, int y) {

            return (PixelData*)(pBase + y * SubjectWidth + x * sizeof(PixelData));            

        }

 

        private void UnlockBits() {

            if (bitmapData == null) return;

            Subject.UnlockBits(bitmapData);

            bitmapData = null;

            pBase = null;

            isLocked = false;

        }

    }

}

 

main.cs

 

//

//  Wave motion simulation. C# version                                                                          //

//  Written by Angel Rapallo 2007                                                                               //

//  I should have called this RAIN simulation, it makes more sense.                                             //

//    'Download by http://www.codefans.net                                                                                                          //

// ORIGINAL:                                                                                                    //

//  Original Algorithm by Don Pattee at http://www.x-caiver.com/Software and Christian Tratz.                   //

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值