I tried to save pictureboxe and picture box in layers Below is the placement of the current form enter image description here

And this image is what I want enter image description here

Try Method 1

public Form1()
    {
        InitializeComponent();
        pictureBox1.Controls.Add(pictureBox2);
        pictureBox2.Location = new Point(0, 0);
        pictureBox2.BackColor = Color.Transparent;
    }
 private void button1_Click(object sender, EventArgs e)
    { 
        string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        pictureBox1.Image.Save(path+"\\image.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
    }

在Picturebox1上叠加PictureBox 2

The result: enter image description here Failed

Try Method 2

private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.DrawImage(this.imageList1.Images[0], 50, 50,393,336);
    }
 private void button1_Click(object sender, EventArgs e)
    { 
        string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        pictureBox1.Image.Save(path+"\\image.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
    }

在图片框中重画1

The result: enter image description here Failed

我希望这两张照片能够完美地重叠并保存.有什么解决方案吗?

推荐答案

据我所知,您希望绘制类似于流行的编辑应用程序的picture box in layers.我们将制作自己的版本,并将其命名为PictureShop.正如Jimi comments 的那样,您只需要一个PictureBox,当您想要在它上面绘制时,调用它的Refresh方法.它的响应方式是触发Paint消息并提供Graphics画布以供绘制.因此,如果我们将图像放在Layers个位图列表中,我们就可以将层一个接一个地绘制出来.显然,这样做是有问题的,但这是一个开始.

layers without transparency

/// <summary>
/// Add layer, then refresh PictureBox to draw.
/// </summary>
private void onButtonDrawClick(object? sender, EventArgs e)
{
    switch ( _clickCount++ ) 
    {
        case 0:
            Layers.Add((Bitmap)Bitmap.FromFile(Path.Combine(ImageFolder, "Image1.png")));
            break;
        case 1:
            Layers.Add((Bitmap)Bitmap.FromFile(Path.Combine(ImageFolder, "Image2.png")));
        break;
    }
    pictureBox1.Refresh();
}

private List<Bitmap> Layers { get; } = new List<Bitmap>();
private void onPicturebox1Paint(object? sender, PaintEventArgs e)
{
    foreach (var bmp in Layers)
    {
        e.Graphics.DrawImage(bmp, pictureBox1.ClientRectangle);
    }
}

如果我们使用的是"其他"应用程序,我们将转到顶层,可能会使用魔杖在顶层图像周围进行 Select ,并使其透明.要使PictureShop执行类似的操作,请在 colored颜色 落在某个值范围内时替换顶部图像中的像素.

现在,公差滑块允许您在保存之前调整数量或透明度.

adjustable transparency

Transparency method

private Bitmap replaceColor(Bitmap bmp, Color targetColor, int tolerance)
{
    if(tolerance == 0) return bmp;
    var copy = new Bitmap(bmp);
    for (int x = 0; x < bmp.Width; x++)
    {
        for (int y = 0; y < copy.Height; y++)
        {
            Color pixelColor = copy.GetPixel(x, y);
            if (localIsWithinTolerance(pixelColor, targetColor, tolerance))
            {
                copy.SetPixel(x, y, Color.Transparent);
            }
        }
    }
    bool localIsWithinTolerance(Color pixelColor, Color targetColor, int tolerance)
    {
        return Math.Abs(pixelColor.R - targetColor.R) <= tolerance &&
                Math.Abs(pixelColor.G - targetColor.G) <= tolerance &&
                Math.Abs(pixelColor.B - targetColor.B) <= tolerance;
    }
    return copy;
}

New paint method

private void onPicturebox1Paint(object? sender, PaintEventArgs e)
{
    Bitmap bmp;
    for (int i = 0; i < Layers.Count; i++) 
    {
        switch (i)
        {
            case 0:
                bmp = Layers[i];
                break;
            case 1:
                bmp = replaceColor(Layers[i], Color.FromArgb(0xF0, 0xF0, 0xF0), trackBarTolerance.Value);
                break;
            default:
                return;
        }
        e.Graphics.DrawImage(bmp, pictureBox1.ClientRectangle);
    }
}

Csharp相关问答推荐

元素存在方法是否损坏

VS Code - C# - dotnet run找不到文件,但我可以打开并编辑它们吗?

始终保留数组中的最后N个值,丢弃最老的

为什么总输出就像12.3没有一分一样?

WPF DataGrid中的三维数据

Thad.Sept()vs Task.Delay().Wait()

从ASP.NET Core中的枚举字段填充 Select 选项时,将默认的第一个选项添加为 Select 元素

如何比较C#中的L和ł(波兰字符)返回TRUE

在try 使用访问服务器上的文件夹时,如何解决CORS错误.NET核心API

泛型参数在.NET 8 AOT中没有匹配的批注

错误CS1061';AuthenticationBuilder';不包含AddOpenIdConnect的定义

在ASP.NET Core 8 MVC中本地化共享视图

Postgres ENUM类型在第一次运行时对Dapper不可见

在集成测试中可以在模拟InMemory数据库中设定数据种子

使用ASP.NET MVC for Lemon Squeezy X-Signature创建散列

如何从Entity Framework Core中填充ListIInterface

如何在使用Google.Drive.apis.V3下载文件/文件夹之前压缩?

具有嵌套属性的IGGroup

使用postman 测试配置了身份的.NET 6应用程序

如何获取我在SQL中输入的值