- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果您是因为标题来到这里,您可能应该跳过问题直接找到答案。原来我的代码中有一个简单的错误。
我正在尝试专门处理索引图像,因为我的项目的主要部分涉及调色板交换。作为更大过程的一部分,我尝试了以下几行代码:
Bitmap raw = ((Bitmap)i).Clone(region, PixelFormat.Format8bppIndexed);
byte transparent = (byte)(Array.FindIndex(raw.Palette.Entries, x => x.A < 128));
// Scan the bitmap for the first opaque pixel on each side
BitmapData bd = raw.LockBits(region, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
基本上,我想裁剪输入的指定区域
,然后对图像的那部分进行低级操作。
转换成功(此时我可以验证它是一个索引位图,但它存储在控件中,因此类型信息丢失)。克隆调用似乎成功了。调试发现raw
的PixelFormat
确实是PixelFormat.Format8bppIndexed
,(Bitmap)i
也是这样.透明调色板索引的计算工作正常。但是随后 raw.LockBits
失败,提示参数无效。
更奇怪的是,如果我删除 .Clone
调用,raw.LockBits
将成功(但其余代码将做错事,因为源代码没有被裁剪)。
根据 this answer , 似乎带有参数的 Clone
产生了对原始数据的某种 View ,而不是实际复制它,因此没有适当的数据供 LockBits
锁定- 它不一定是连续的。但是我如何明确复制该区域?通常的 Graphics.DrawImage
方法不会直接起作用,因为所需的目标图像已编入索引,所以我无法为其获取 Graphics。
我真的必须在 RGB 模式下完成所有工作然后再转换回来吗?肯定有更优雅的东西?
最佳答案
根据@Peter Duniho 的评论和一些进一步的测试和调试,我确定实际上我只是在代码中有一个错误。结论:.Clone() 方法工作得很好,正是这里所需要的:
Bitmap cropped = original.Clone(region, PixelFormat.Format8bppIndexed);
或者更笼统一点,
Bitmap cropped = original.Clone(region, original.PixelFormat);
其中 region
是所需的裁剪 Rectangle
。我的代码的问题是,在随后的 LockBits
代码中,region
不正确 - 要锁定整个裁剪图像,我想要与 相同的宽度和高度>矩形
,但 X 和 Y 为 (0, 0)。
经过更多测试后,我针对此处可能遇到的错误制定了以下注释:
如果您尝试使用不适合 Bitmap
矩形的 region
- 即 X
或 Y
为负数,或者 X + Width
超过了 Bitmap
的 Width
,或者 Y + Height
超出了 Bitmap
的高度 - 它不会被裁剪到原始 Bitmap
的边界,而是会发生异常。 负的宽度和高度值也是无效的。
如果 Bitmap.LockBits
的 region
宽度为零、高度为零或不适合,它将引发 System.ArgumentException
。消息只是“参数无效”。 无论哪种方式,因此您需要找出问题所在。
如果 Bitmap.Clone
的 region
宽度为零或高度为零,您将再次得到 System.ArgumentException
,这次带有实际信息性错误消息。但是,如果它不适合,您将得到一个 System.OutOfMemoryException
。这有点有趣,因为 0 的宽度和高度值显然得到了显式检查,但负值却没有。
所以也许我们应该使用一个小函数来防止这种情况,比如:
// Create a cropped `region` of the `original` Bitmap as a new bitmap,
// preserving the original pixel format. If negative Width or Height
// are provided for the clip region and `flipNegative` is set, the result
// is flipped accordingly.
public Bitmap crop(Bitmap original, Rectangle region, bool flipNegative) {
Rectangle bounds = new Rectangle(new Point(0, 0), original.Size);
if (region.Width == 0 || region.Height == 0) { return null; }
// Normalize width and height parameters,
// and track whether we might need to flip.
bool flipHorizontal = region.Width < 0;
bool flipVertical = region.Height < 0;
if (flipHorizontal)
{
region.X += region.Width;
region.Width = -region.Width;
}
if (flipVertical)
{
region.Y += region.Height;
region.Height = -region.Height;
}
// Ensure we have a valid clipping rectangle, and make the GDI call.
if (!region.IntersectsWith(bounds)) { return null; }
region.Intersect(bounds);
Bitmap result = original.Clone(region, original.PixelFormat);
// Flip the result as appropriate.
if (flipHorizontal && flipNegative)
{
result.RotateFlip(RotateFlipType.RotateNoneFlipX);
}
if (flipVertical && flipNegative)
{
result.RotateFlip(RotateFlipType.RotateNoneFlipY);
}
return result;
}
(我提供了 flipNegative
参数,因为可以想象,您可能想要任一语义。)
至于LockBits
,这里没有必要,尽管它对于更底层的操作仍然有用。
关于c# - 如何有效裁剪 *indexed* 位图并获得 *indexed* 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27596660/
是否有可能(在 cmd 批处理 ffmpeg 中)拍摄宽度已知(1920px)但高度未知的图像,如果超过,则将高度裁剪为特定的值?基本上是最大高度裁剪。 我玩过缩放和裁剪,但我无法得到我需要的结果。任
我有两个 SpatialPolygonsDataFrame文件:dat1、dat2 extent(dat1) class : Extent xmin : -180 xmax
我在 TensorFlow 上实现了全卷积网络。它使用编码器-解码器结构。训练时,我始终使用相同的图像大小(224x224,使用随机裁剪)并且一切正常。 在干扰阶段,我想一次预测一张图像,因为我想使用
我在 TensorFlow 上实现了全卷积网络。它使用编码器-解码器结构。训练时,我始终使用相同的图像大小(224x224,使用随机裁剪)并且一切正常。 在干扰阶段,我想一次预测一张图像,因为我想使用
我有一个需要裁剪的 View 。我有 4 个 View 显示在主视图上查看的视频 subview 。由于视频比例,我需要裁剪使视频成为正方形而不是矩形的 View 。这是我的代码: - (void)v
我正在构建一个使用 Parse 作为我的后端的网络应用程序。 部分注册过程涉及用户上传和裁剪图片,然后我将其传递到我的数据库(图片是用户个人资料的一部分,类似于您在 Twitter 上所做的)。 我已
我正在制作一个基于立方体的游戏(一切都是立方体),目前正在尝试通过不在视野之外绘制东西来优化它。 以下内容仅适用于 x 和 y 平面,稍后我会担心 z ......所以现在只进行侧面裁剪。 我知道我自
我正在尝试在 iOS 上实现单指图像缩放/裁剪。类似于柯比·特纳的单指旋转。我正在寻找现有的库,或者如果您可以帮助我处理代码本身,那就太好了。 最佳答案 我不太清楚你所说的一指裁剪是什么意思,但我为
从这里: http://www.kylejlarson.com/blog/2011/how-to-create-pie-charts-with-css3/ .pieContainer
我已经设置了一个 SKScene 用作 SKReferenceNode。雪橇是一个 SKSpriteNode,在引用节点场景中定义了一个自定义类,所有的狗都是雪橇 Sprite 的 child 。自定
我有一个库,其中包含一些图像处理算法,包括感兴趣区域(裁剪)算法。使用 GCC 编译时,自动矢量化器会加速很多代码,但会降低 Crop 算法的性能。是否有标记某个循环以被矢量化器忽略的方法,或者是否有
代码位于 http://jsfiddle.net/rSSXu/ Child #parent { margin-left:auto; margin-right:auto;
我搜索了很多以删除不需要的空间,但找不到。我只找到可用于删除黑白背景空间的链接。但我的背景图片可以是任何东西。所以,如果我有这些图片, 我如何提取我需要的图像部分。例如, 最佳答案 这是我对你的问题的
我正在尝试将 CMSampleBufferRef 中的图像裁剪为特定大小。我正在执行 5 个步骤 - 1. 从 SampleBuffer 获取 PixelBuffer 2. 将 PixelBuffer
我读到它是自动的,但在我的案例中似乎没有发生。使用 UIImagePickerController 并将 allowsEditing 设置为 YES 我得到了带有裁剪方形叠加层的编辑 View ,但是
我正在寻找一种高效的方法来裁剪二维数组。考虑这个例子: 我有一个构成 100x100 网格的二维数组。我只想返回其中的一部分,60x60。这是一个“a”方法的示例,但我正在寻找指向执行此操作的最高性能
我有一个接受 UIImage 的类,用它初始化一个 CIImage,如下所示: workingImage = CIImage.init(image: baseImage!) 然后使用图像以 3x3 的
我正在尝试显示来自 mysql 数据库的图像。有些图像显示正确,但有些图像在底部显示为剪切/裁剪,裁剪部分仅显示为空白,当它成为图像的一部分时,您真的无法摆脱。 CSS 无法解决这个问题,使用 ima
我有个问题。我有什么理由不应该使用这个 Intent: Intent intent = new Intent("com.android.camera.action.CROP"); 为了裁剪我刚刚拍摄的
我有一些代码可以调整图像大小,因此我可以获得图像中心的缩放 block - 我使用它来获取 UIImage 并返回一个小的方形表示图片,类似于在照片应用程序的相册 View 中看到的内容。 (我知道我
我是一名优秀的程序员,十分优秀!