- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道这听起来微不足道,但我目前陷入困境。
我有各种格式的图像作为输入。 Gray16、Gray8、Indexed8 等。我需要的是将它们转换为 RGB24 格式,到目前为止我已经做到了
public WriteableBitmap ConvertToRgb(WriteableBitmap source)
{
<-- create the new bitmap of the same width/height as the source with specific RGB24 format-->
var newBitmap = new WriteableBitmap(source.PixelWidth, source.PixelHeight, source.DpiX, source.DpiY, PixelFormats.Rgb24, source.Palette);
<-- create my pixel array -->
byte[] pixels = new byte[
source.PixelHeight * source.PixelWidth *
source.Format.BitsPerPixel / 8];
<-- copy my original pixels into the array) -->
source.CopyPixels(pixels, source.PixelWidth * source.Format.BitsPerPixel / 8, 0);
<-- write the pixels into the new image -->
newBitmap.WritePixels(
new Int32Rect(0, 0, newBitmap.PixelWidth, newBitmap.PixelHeight),
pixels,
newBitmap.PixelWidth * newBitmap.Format.BitsPerPixel/8,
0);
return newBitmap;
}
此方法抛出异常“缓冲区大小不足”
最佳答案
第一:RGB 图像的默认格式是(违反直觉的)ARGB 的 BGR24 或 BGRA32。这是因为系统的字节序 ( see here for why this happens )
第二:当你使用这种方法时,你根本不会转换PixelFormat。您只需获取所有像素,将它们转换为字节数组并将这些字节用于新的像素格式 - 这将完全不同地解释字节。因此,如果您的源图像在 BGRA 中并且您转换为 RGB24,则第一个源像素的蓝色分量将被解释为第一个目标像素的红色分量,依此类推 - 第一个源 alpha 字节将被解释为第一个第二个像素的红色字节(不完全正确,但更容易理解。正确的映射是 a>b, r>g, g>r, r>b2... 因为颜色如何映射到与它们是如何命名的)。这也是您的缓冲区大小不匹配的原因 - 因为不同的像素格式需要不同的字节数。
WriteableBitmap 的 CopyPixels 和 WritePixels 完全不知道底层的 PixelFormat 并完全忽略它。只有在源和目标具有完全相同的 PixelFormat 和 Palette 或者您手动重新计算和重新排列缓冲区中的所有字节时,这才有效。
你要找的东西要容易得多:它叫做 FormatConvertedBitmap
.这是一个 BitmapSource,专门用于更改图像的 PixelFormat。下面这行应该完成将所有内容转换为适当的 RGBA32 图像的工作(仅将 Bgr24 用于 RGB)。
var rgba32 = new FormatConvertedBitmap(source, PixelFormats.Bgra32, source.Palette,0);
关于c# - 从一种 PixelFormat 转换为另一种 WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62592706/
如何从 PixelFormat 获取像素长度(以字节为单位)枚举?我想使用 native 方法处理图像像素,但如果我不知道像素的偏移量,我该如何遍历图像。 代码: let formRgbCube (i
正如标题所说,有人知道什么是RGBX_8888像素格式吗?和 RGBA_8888 有什么区别? RGBA_8888 是否提供 alpha channel 但 RGBX_8888 不提供? 不幸的是,A
我对 PixelFormat 感到困惑在安卓系统上。 我的设备是摩托罗拉 Defy。 我有两个问题: 在 Android 2.3 上 getWindowManager().getDefaultDisp
我尝试从位图 (System.Drawing.Bitmap) 中获取所有字节值。因此我锁定字节并复制它们: public static byte[] GetPixels(Bitmap bitmap){
我知道这听起来微不足道,但我目前陷入困境。 我有各种格式的图像作为输入。 Gray16、Gray8、Indexed8 等。我需要的是将它们转换为 RGB24 格式,到目前为止我已经做到了 public
Accord.NET PointsMarker.cs似乎支持 PixelFormat Format32bppArgb。为什么这会捕获 UnsupportedImageFormatException?
我正在尝试使用 pictureBox->Image (Windows Forms) 来显示 cv::Mat 图像 (openCV)。我想这样做而不将图像保存为文件(因为我想每 100 毫秒重置一次图像
我正在使用以下方法将 BitmapSource 转换为 Bitmap: internal static Bitmap ConvertBitmapSourceToBitmap(BitmapSource
我们的目标是用来自 Kinect 的红外图像制作一部电影。我们正在使用 AForge videowriter,我们已经有了处理普通 RGB 流的工作代码。 (RgbResolution640x480F
我正在尝试使用该函数设置渲染格式 CCTexture2D.setDefaultAlphaPixelFormat(Bitmap.Config.ARGB_8888); 但它给我一个错误 找不到符号方法 s
有没有办法从 PixelFormat 类型的 codecContext.pix_fmt 中获取像素的位数?我使用开关手动完成: switch (OUT_IMAGE_FORMAT) { case
考虑以下设置:我在磁盘上有一些图像。我无法修改这些图像,因为它们属于另一个项目。我刚读完。 我需要对它们进行处理以使其在我的应用中看起来最佳。我的意思是像素着色器已处理。当我完成处理后,输出图像将用作
我发现更改 SurfaceView 中的像素格式对帧速率有很大影响。但是,我似乎无法找到一种方法来根据每个设备选择最佳格式。 例子: @Override public void surfaceCrea
我需要以原始 bpp 格式处理和保存图像(1 - BnW,8 - 灰色,24 - 彩色)。处理后我总是有 24bpp(由于使用 Aforge.Net 处理)。我将原始 bpp 传递给保存函数,我正在使
我目前正在使用 C# 进行一些图像处理,我对 Bgr32 像素格式感到困惑。以下说明来自microsoft msdn:http://msdn.microsoft.com/en-us/library/s
我正在寻找渲染 DrawingVisual (示例中的可视化)使用 RenderTargetBitmap 到位图使用 View 将此位图设置为 Canvas 的背景如下: var bmp = new
在System.Drawing中,我们从Image对象中检索PixelFormat,但SkiaSharp.SkImage不提供API来查找解码图像的PixelFormat。是否有任何其他解决方法来查找
我正在尝试在 AVCaptureConnection 上设置像素格式类型,但它会终止所有 RGB 设置,但适用于 kCVPixelFormatType_32BGRA。 Terminating app
我有一个具有以下 View 结构的游戏应用程序。首先,我有一个空的 FrameLayout,如下所示: FrameLayout game_frame_layout = new FrameLayout(
我想使用函数 CGDisplayStreamCreateWithDispatchQueue。 文档告诉我为“pixelFormat”使用以下可能的值:(参数需要是一个Int32) The desire
我是一名优秀的程序员,十分优秀!