C# 对图像进行锐化 锐化的算法

   /////锐化
        ///
        /// 对图像进行锐化,参考:http://msdn.microsoft.com/en-us/library/ms534073(v=vs.85).aspx
        ///

        /// 需要锐化的区域,会对该值进行边界的修正并返回.
        /// 指定高斯卷积核的半径,有效范围[0,255],因为这个锐化算法是以高斯模糊为基础的,所以他的速度肯定比高斯模糊妈妈
        /// 指定锐化的程度,0表示不锐化。有效范围[0,255].
        ///
        public static void UsmSharpen(this Bitmap Bmp, ref Rectangle Rect, float Radius, float Amount)
        {
            int Result;
            IntPtr UnSharpMaskEffect;
            SharpenParams sharpenParams;
            if ((Radius < 0) || (Radius > 255))
            {
                throw new ArgumentOutOfRangeException("参数Radius必须在[0,255]范围内");
            }
            if ((Amount < 0) || (Amount > 100))
            {
                throw new ArgumentOutOfRangeException("参数Amount必须在[0,255]范围内");
            }
            sharpenParams.Radius = Radius;
            sharpenParams.Amount = Amount;
            Result = GdipCreateEffect(UsmSharpenEffectGuid, out UnSharpMaskEffect);
            if (Result == 0)
            {
                IntPtr Handle = Marshal.AllocHGlobal(Marshal.SizeOf(sharpenParams));
                Marshal.StructureToPtr(sharpenParams, Handle, true);
                GdipSetEffectParameters(UnSharpMaskEffect, Handle, (uint)Marshal.SizeOf(sharpenParams));
                GdipBitmapApplyEffect(Bmp.NativeHandle(), UnSharpMaskEffect, ref Rect, false, IntPtr.Zero, 0);
                GdipDeleteEffect(UnSharpMaskEffect);
                Marshal.FreeHGlobal(Handle);
            }
            else
            {
                throw new ExternalException("不支持的GDI+版本,必须为GDI+1.1及以上版本,且操作系统要求为Win Vista及之后版本.");
            }
        }

你可能感兴趣的:(C# 对图像进行锐化 锐化的算法)