您当前的位置: 首页 >  c#

寒冰屋

暂无认证

  • 2浏览

    0关注

    2286博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

使用GDI和未经检查的代码在C#中反射图像

寒冰屋 发布时间:2022-09-08 20:00:00 ,浏览量:2

目录

代码

如何使用代码

样本结果。

代码

该代码使用不安全代码来加速不透明渐变的创建,因此必须在启用不安全标志的情况下编译代码。

代码大部分是不言自明的。

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             
关注
打赏
1665926880
查看更多评论
0.0444s