none
WPF中实现Photoshop的功能,如何把图像挖空? RRS feed

  • 问题

  • 按需求需要实现PS 部分效果,我在层上画了一些线,形状,图片,我要把图形挖空,清除掉指定的区域,该怎么实现 ?

    2013年9月6日 1:38

答案

  • 您好,

    根据您的描述,建议您参考下面的示例工程:

    #Erase to Transparency (WriteableBitmap)
    http://code.msdn.microsoft.com/Erase-to-Transparency-caa261a1

    这个Demo 可以加载一幅图片之后,使用鼠标涂抹掉图片,就像橡皮擦功能一样。

    以下的方法可以根据您鼠标的位置,擦除图片的一部分:

    static public void Rubber(int x, int y, int RubberWidth, int rubberHeight, WriteableBitmap image)
    {
                var halfRubberWidth = RubberWidth / 2;
                var halfRubberHeight = rubberHeight/2;
    
                if (x > (image.PixelWidth - halfRubberWidth) ||
                    y > (image.PixelHeight - halfRubberHeight) ||
                    x < halfRubberWidth || y < halfRubberHeight) return; // Boundaries
    
                int stride = image.PixelWidth * image.Format.BitsPerPixel / 8;
                int byteSize = stride * image.PixelHeight * image.Format.BitsPerPixel / 8;
                var ary = new byte[byteSize];
                image.CopyPixels(ary, stride, 0);
    
                var curix = ((y-10) * stride) + ((x-10) * 4 );
    
                for (var iy = 0; iy < rubberHeight; iy++)
                {
                    for (var ix = 0; ix < RubberWidth; ix++)
                        for (var b = 0; b < 4; b++)
                        {
                            ary[curix] = 0;
                            curix++;
                        }
                    curix = curix + stride - (RubberWidth * 4);
                }
    
                image.WritePixels(new Int32Rect(0, 0, image.PixelWidth, image.PixelHeight), ary, stride, 0);
    } 

     


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年9月9日 2:09
    版主