代码之家  ›  专栏  ›  技术社区  ›  ksliman

将卷曲效果应用于静态图像C#

  •  2
  • ksliman  · 技术社区  · 12 年前

    任何人都曾编写过C#GDI+函数来卷曲BITMAP的一角。我需要能够拍摄静态图像,并在右下角应用剥离效果。我需要用C#来完成,我所有的搜索结果都指向CSS3/FLASH/SilverLight虚拟书类型的例子。我只想创建一个具有卷曲角的静态图像并保存文件。

    有什么想法吗?

    好的,所以我用照片商店制作了这张照片,这样我就可以向你展示我正在努力实现的目标

    我开始此图像

    enter image description here

    我想写一些C#代码来生成这个图像 enter image description here 最终的结果只是一个没有动画的图像,并且什么都不做。任何想法。

    1 回复  |  直到 12 年前
        1
  •  4
  •   Jeremy Thompson    11 年前

    有一些很好的工具可以做到这一点,例如 Fred's ImageMagick plugin script ,但这里是一个C#版本。

    using System.Drawing.Imaging;
    
    public partial class ImagePeelEffect : Form
    {
    string WorkingDirectory = @"C:\temp\";
    public ImagePeelEffect()
    {
       InitializeComponent();
    }
    
    private void ImagePeelEffect_Load(object sender, EventArgs e)
    {
        picBefore.Image = Image.FromFile(WorkingDirectory + "\\before.jpg");    
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        //create a image object containing the photograph to add page peel effect
        Image imgPhoto = Image.FromFile(WorkingDirectory + "\\before.jpg");
        int phWidth = imgPhoto.Width;
        int phHeight = imgPhoto.Height;
    
        //create a Bitmap the Size of the original photograph
        Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
    
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
    
        //load the Bitmap into a Graphics object 
        Graphics grPhoto = Graphics.FromImage(bmPhoto);
    
        //create a image object containing the PagePeel
        Image imgPagePeel = new Bitmap(WorkingDirectory + "\\PagePeel.bmp");
        int ppWidth = imgPagePeel.Width;
        int ppHeight = imgPagePeel.Height;
    
        //Set the rendering quality for this Graphics object
        grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    
        //Draws the photo Image object at original size to the graphics object.
        grPhoto.DrawImage(
            imgPhoto,                               // Photo Image object
            new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
            0,                                      // x-coordinate of the portion of the source image to draw. 
            0,                                      // y-coordinate of the portion of the source image to draw. 
            phWidth,                                // Width of the portion of the source image to draw. 
            phHeight,                               // Height of the portion of the source image to draw. 
            GraphicsUnit.Pixel);                    // Units of measure 
    
    
        //The first step in manipulating the PagePeel image is to replace 
        //the background color with one that is transparent (Alpha=0, R=0, G=0, B=0)
        //to do this we will use a Colormap and define ImageAttributes with a RemapTable
        ImageAttributes imageAttributes = new ImageAttributes();
        ColorMap colorMap = new ColorMap();
    
        //My PagePeel was defined with a background of 100% Green this will
        //be the color we search for and replace with transparency
        colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
        colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
    
        //Set the Remap Table with the old and new color map
        ColorMap[] remapTable = { colorMap };
        imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
    
    
        //For this example we will place the PagePeel in the bottom right
        //hand corner of the photograph
        int xPosOfPp = phWidth - ppWidth;
        int yPosOfPp = phHeight - ppHeight + 1;
    
        grPhoto.DrawImage(imgPagePeel,
            new Rectangle(xPosOfPp, yPosOfPp, ppWidth, ppHeight),  //Set the detination Position
            0,                  // x-coordinate of the portion of the source image to draw. 
            0,                  // y-coordinate of the portion of the source image to draw. 
            ppWidth,            // PagePeel Width
            ppHeight,           // PagePeel Height
            GraphicsUnit.Pixel, // Unit of measurment
            imageAttributes);   //ImageAttributes Object
    
        //Replace the original photgraphs bitmap with the new Bitmap
        imgPhoto = bmPhoto;
        grPhoto.Dispose();
    
        //save new image to file system.
        imgPhoto.Save(WorkingDirectory + "\\after.jpg", ImageFormat.Jpeg);
        imgPhoto.Dispose();
        imgPagePeel.Dispose();
    
        //Show the After image
        picAfter.Image = Image.FromFile(WorkingDirectory + "\\after.jpg");
    
    }
    

    PagePeel.bmp:

    enter image description here

    前后结果:

    enter image description here

    使现代化

    这是一个使用透明页面剥离覆盖的版本,因此您不需要将“绿色屏幕”转换为不可见。这种方法的优点是当原始照片包含绿色RGB(0255,0)时,它不会变成透明的:

    透明页面Peel.png:

    enter image description here

    private void button2_Click(object sender, EventArgs e)
    {
        //create a image object containing the photograph to add page peel effect
        Image imgPhoto = Image.FromFile(WorkingDirectory + "\\before.jpg");
        int phWidth = imgPhoto.Width;
        int phHeight = imgPhoto.Height;
    
        //create a Bitmap the Size of the original photograph
        Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
    
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
    
        //load the Bitmap into a Graphics object 
        Graphics grPhoto = Graphics.FromImage(bmPhoto);
    
        //create a image object containing the PagePeel
        Image imgPagePeel = new Bitmap(WorkingDirectory + "\\transparentPagePeel.png");
        int ppWidth = imgPagePeel.Width;
        int ppHeight = imgPagePeel.Height;
    
        //Set the rendering quality for this Graphics object
        grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    
        //Draws the photo Image object at original size to the graphics object.
        grPhoto.DrawImage(
            imgPhoto,                               // Photo Image object
            new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
            0,                                      // x-coordinate of the portion of the source image to draw. 
            0,                                      // y-coordinate of the portion of the source image to draw. 
            phWidth,                                // Width of the portion of the source image to draw. 
            phHeight,                               // Height of the portion of the source image to draw. 
            GraphicsUnit.Pixel);                    // Units of measure 
    
        //For this example we will place the PagePeel in the bottom right
        //hand corner of the photograph
        int xPosOfPp = phWidth - ppWidth;
        int yPosOfPp = phHeight - ppHeight + 1;
    
        grPhoto.DrawImage(imgPagePeel,
            new Rectangle(xPosOfPp, yPosOfPp, ppWidth, ppHeight),  //Set the detination Position
            0,                  // x-coordinate of the portion of the source image to draw. 
            0,                  // y-coordinate of the portion of the source image to draw. 
            ppWidth,            // PagePeel Width
            ppHeight,           // PagePeel Height
            GraphicsUnit.Pixel, // Unit of measurment
            null);   //ImageAttributes Object
    
        //Replace the original photgraphs bitmap with the new Bitmap
        imgPhoto = bmPhoto;
        grPhoto.Dispose();
    
        //save new image to file system.
        imgPhoto.Save(WorkingDirectory + "\\after1.jpg", ImageFormat.Jpeg);
        imgPhoto.Dispose();
        imgPagePeel.Dispose();
    
    
        picAfter.Image = Image.FromFile(WorkingDirectory + "\\after1.jpg");
    
    }