目录
代码
如何使用代码
样本结果。
代码该代码使用不安全代码来加速不透明渐变的创建,因此必须在启用不安全标志的情况下编译代码。
代码大部分是不言自明的。
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace BitmapTools
{
public static class BitmapExtensions
{
public struct Pixel
{
public byte B;
public byte G;
public byte R;
public byte A;
}
///
/// Creates a bitmap with the original (bm) image on top and under that
/// a reflected image.
///
/// original image
/// Percentage of height of the original image
/// that the reflection must be.
/// Angle that the reflection must shear.
/// Positive to the left.
/// If true then the reflection get a little blur.
///
public static Bitmap ReflectImage(Bitmap bm, float verticalCompress,
int offsetAngle, bool colorBlur = true)
{
// calculating the size of the resulting bitmap and the reflected image.
int reflectionHeight = (int)(bm.Height - (bm.Height * verticalCompress / 100));
int newwidth = bm.Width;
int newheight = bm.Height + reflectionHeight;
int offset;
offset = (int)(Math.Sin(offsetAngle * Math.PI / 180) * reflectionHeight);
newwidth = bm.Width + Math.Abs(offset);
Bitmap resultBitmap = new Bitmap(newwidth, newheight, PixelFormat.Format32bppArgb);
resultBitmap.SetResolution(bm.VerticalResolution, bm.HorizontalResolution);
// create the bitmap with a positive of the original
Bitmap reflectionSrc =
new Bitmap(bm.Width, reflectionHeight, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(reflectionSrc))
{
g.DrawImage(bm, new Rectangle(0, 0, bm.Width, reflectionHeight),
0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel);
}
// now flip the image to get a mirror image.
reflectionSrc.RotateFlip(RotateFlipType.RotateNoneFlipY);
// now fade to transparent. Can be done with managed code but too slow.
// add conditionally add some blur.
BitmapData bmd = reflectionSrc.LockBits(
new Rectangle(Point.Empty, reflectionSrc.Size),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
unchecked
{
Pixel* ps = (Pixel*)bmd.Scan0.ToPointer();
for (int y = 0; y < reflectionSrc.Height; y++)
{
int alpha = (int)((float)(1 - (y / (float)reflectionHeight)) * (float)0xFF);
for (int x = 0; x < reflectionSrc.Width; x++, ps++)
{
ps->A = (byte)alpha;
if (colorBlur)
{
ps->R = (byte)((ps->R * 3 + ps->G * 2 + ps->B * 2) / 7);
ps->G = (byte)((ps->R * 2 + ps->G * 3 + ps->B * 2) / 7);
ps->B = (byte)((ps->R * 2 + ps->G * 2 + ps->B * 3) / 7);
}
}
}
}
}
reflectionSrc.UnlockBits(bmd);
// now create the canvas to draw the original and the flipped image with
// the shear factor
using (Graphics gr = Graphics.FromImage(resultBitmap))
{
// draw original on top of the result image
gr.DrawImage(bm, offset
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?