【问题标题】:C# WinForms | Picturebox white bleeding right & bottom edgesC# WinForms | Picturebox白色出血右侧和底部边缘
【发布时间】:2021-12-04 11:27:02
【问题描述】:

你好 Stackoverflow 社区。​​p>

我目前正在开发一个具有光标区域放大功能的应用程序,供用户选择颜色。

但是,我的问题是 Picturebox 在右侧和底部有白色边缘,即使捕获的图像只有一种深色。

捕获的屏幕为 10x10 像素,Picturebox 为 80x80 像素,并且 SizeMode 设置为 StretchImage。

我检查了原始捕获的图像是否可能已经包含这些边缘,方法是保存它并在 Photoshop 中检查它。但是原始捕获的图像很好,所以图片框一定很奇怪。

在这里您可以看到它的外观(鼠标光标和捕获区域 [绿色矩形] 只是在屏幕截图上绘制以进行演示,因为我无法屏蔽我的光标,是的 - 绿色区域是绘制方式太大了,应该只有10x10像素^^)

这可能与 Image 在内部拉伸的方式有关吗?如果是这样,是否有任何不太复杂的方法来解决它?

感谢您提前提供的任何帮助 :)

//编辑:我认为这实际上是关于拉伸 我找到了这个主题Image after resize has white border 但我不知道 GetSize() 方法是什么,或者它在哪里(来自)

祝你有美好的一天!

【问题讨论】:

  • 检查图片框上的BorderStyle 属性。如果是 3D,则可能会导致此问题
  • 你好,我已经检查过了,但我认为这是关于拉伸的。它将 10x10 像素拉伸为 80x80 我找到了这个主题 stackoverflow.com/questions/20626985/… 但我不知道 GetSize() 方法是什么,或者它在哪里(来自)

标签: c# winforms picturebox stretching


【解决方案1】:

我找到了边缘here 的解决方案,正如您在第一个屏幕中看到的那样。但是,在第二个屏幕中,您可以看到 2x2 像素捕获,使用找到的“解决方案”拉伸到 80x80 像素,将混合颜色,因为它最终实际上是 80x80 像素,而我想让它显示原始像素,所以,在这种情况下,3 个黑色和 1 个白色。现在我更卡了^^ 我想我应该改为从捕获的图像中读取单个像素的像素颜色并在预览图片框中设置颜色,或者在只有 2x2 像素的情况下制作 4 个面板

public new Image Resize(Image image, int targetWidth, int targetHeight)
{
    var resizedImage = new Bitmap(targetWidth, targetHeight);

    using (var graphics = Graphics.FromImage(resizedImage))
    {
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        var attributes = new ImageAttributes();
        attributes.SetWrapMode(WrapMode.TileFlipXY);
        var destination = new Rectangle(0, 0, targetWidth, targetHeight);
        graphics.DrawImage(image, destination, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
    }

    return resizedImage;
}

//编辑: 好的,我想我会使用 Get/SetPixel 或 Panels(用于小捕获),就像我在右边所做的那样,左边仍然是 80x80 Picturebox

【讨论】:

  • 使用 InterpolationMode.NearestNeighbor
  • 非常感谢
猜你喜欢
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
  • 2022-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多