c# - 将 curl 效果应用于静态图像 C#

标签 c# image-processing gdi+

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

有什么想法吗?

好的,我用 photoshop 制作了图像,以便我可以向您展示我想要实现的目标

我开始这张图片

enter image description here

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

最佳答案

有一些很好的工具可以做到这一点,例如 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(0,255,0)时,它不会变成透明:

TransparentPagePeel.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");

}

关于c# - 将 curl 效果应用于静态图像 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22045873/

相关文章:

c# - 如何通过链接使元素出现在屏幕中央?

c# - 检查对象类型是否继承了抽象类型

python - NumPy - 使用强度值矩阵的图像(矩阵)阈值处理。

.net - 是否有具有相同公共(public)接口(interface)的 System.Drawing.Graphics 的直接替代品?

c# - 如何在线条图中找到圆弧或开放曲线?

c# - 在 Paint 事件处理程序中处理图形元素

c# - 使用 C# 在 Autocad 中将 3D 绘图转换为 2D 绘图

c# - 是否有机会使用 Linq (C#) 获得唯一记录?

image-processing - 如何使用 OpenCV 像矩阵一样操作图像?

c# - 查找形状上的角/边(可以定义该形状的最小顶点)