- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个与我的屏幕分辨率(1280x800,每像素 24 位)相对应的内存缓冲区,其中包含我的 24bpp 屏幕内容。我想将其转换为 8-bpp(即 Windows 中的半色调调色板)。我目前这样做:1. 使用 CreateDIBSection 分配一个新的 1280x800 24-bpp 缓冲区并将其作为 DC 以及普通内存缓冲区访问2. 使用 memcpy 从我的原始缓冲区复制到步骤 1 中的这个新缓冲区3.使用BitBlt让GDI进行颜色转换
我想避免步骤 2 的额外 memcpy。为此,我可以想到两种方法:
一个。将我原来的 mem buf 包装在一个 DC 中以直接从它执行 BitBlt
编写我自己的 24-bpp 到 8-bpp 颜色转换。我找不到有关 Windows 如何实现此半色调颜色转换的任何信息。此外,即使我知道了,我也不会使用 BitBlt 可以访问的 GDI 加速功能。
那么我该怎么做 (a) 或 (b)?
谢谢!
最佳答案
好的,解决问题的两个部分。
以下代码展示了如何获取位图中的像素、更改它们并将它们放回位图中。你总是可以生成一个正确大小和格式的虚拟位图,打开它,复制你的数据,然后你有一个包含你的数据的位图对象:
private void LockUnlockBitsExample(PaintEventArgs e)
{
// Create a new bitmap.
Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int bytes = bmpData.Stride * bmp.Height;
byte[] rgbValues = new byte[bytes];
// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
// Set every third value to 255. A 24bpp bitmap will look red.
for (int counter = 2; counter < rgbValues.Length; counter += 3)
rgbValues[counter] = 255;
// Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
// Unlock the bits.
bmp.UnlockBits(bmpData);
// Draw the modified image.
e.Graphics.DrawImage(bmp, 0, 150);
}
要将内容转换为 8bpp,您需要使用 System.Drawing.Imaging.ColorMatrix 类。我手边没有正确的半色调矩阵值,但是这个示例灰度和值的调整应该让您了解效果:
Graphics g = e.Graphics;
Bitmap bmp = new Bitmap("sample.jpg");
g.FillRectangle(Brushes.White, this.ClientRectangle);
// Create a color matrix
// The value 0.6 in row 4, column 4 specifies the alpha value
float[][] matrixItems = {
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 0.6f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
// Create an ImageAttributes object and set its color matrix
ImageAttributes imageAtt = new ImageAttributes();
imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
// Now draw the semitransparent bitmap image.
g.DrawImage(bmp, this.ClientRectangle, 0.0f, 0.0f, bmp.Width, bmp.Height,
GraphicsUnit.Pixel, imageAtt);
imageAtt.Dispose();
稍后我将尝试更新半色调的矩阵值,其中可能有很多 0.5 或 0.333 值!
关于c# - 如何将现有内存缓冲区包装为 GDI 的 DC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/91511/
我是 dc.js 的新手,我对 dc 的灵 active 有一些疑问。 首先,我一直在寻找答案,但还没有找到任何答案。 1) 我正在使用 dc.sunburst 图表。我想知道是否有可能创建 Zoom
谁能解释一下 ldap 字符串部分的构成。 我有一个是: string strSQL = "SELECT mail FROM 'LDAP://DC=amrs,DC=win,DC=ml,dc=COM'
他们已经就如何将 dc.js 简单地用作图形库(例如绘制饼图、条形图...)而不需要交叉过滤器进行了各种讨论,例如,如果您只想在数据已经是一个(标签,值)。 我没有找到任何关于如何做到这一点的例子。这
当我点击数据表中的一行时,我需要过滤其他图表。 我做了 my_table.on('pretransition', function (table) { table.selectAll('td
我想显示小切片( chart.minAngleForLabel(0.05) )的标签,避免文本重叠。 我添加了一个将标签移向外边缘的renderlet: .on('renderlet', func
如何在新列的 dc.js 数据表中添加带有事件监听器的按钮。 如果可能的话,我基本上想添加自定义 html。 最佳答案 添加按钮只是返回列的 html 的问题 dc.dataTable (dom) .
这个问题在这里已经有了答案: Initial Range selection in DC.js chart (1 个回答) 关闭 7 年前。 我创建了 this可视化,并希望在解释文本中使用预定义的
我用 dc.js 制作了一个热图,我想知道 dc.js 中是否有热图的键或图例函数。我在互联网上搜索过,但似乎找不到内置的方法,那么有没有其他人解决过这个问题? 最佳答案 我遇到了同样的任务,我所做的
我有一个具有以下模型的数据源: [ { days : 3, value : 500 }, { days : 30,
如果过滤后 dc.js 中的行图值为零,我们如何隐藏。我们有这样的代码: var kurum=data.dimension(function(d){return ""+ d.KURUM;});
自 1.4.0-alpha.06 版起,Crossfilter 支持数组维度 https://github.com/crossfilter/crossfilter/wiki/API-Reference
我希望在 dc.js 的多维数据集中绘制与连续变量关联的几个直方图。虽然使用 dc.barChart 组件很容易实现这一点,但我希望对这些直方图进行面积归一化。在我的例子中,bin 宽度是统一的,所以
我目前正在尝试弄清楚如何使用 DJ.js 和 D3.js 计算要显示的唯一记录数 数据集如下所示: id,name,artists,genre,danceability,energy,key,loud
在此图表上:http://junklogic.com/dcjs/barpie/index-controls.html 我的示例 JSON 文件中有与严重性评级相对应的图例,该文件代表我将在生产中使用的
我有以下数据。 我可以使用 dc.js 创建一个吗? 有人可以帮助我使用 dc js 创建直方图吗? 搜索了整个论坛但除了这个post 找不到有用的东西. 日期: numbers11012035242
以下是一组数据: var data = [{ machine1: [ {x:"20/12/2014", y:2}, {x:"21/12/2014", y:10} ]},{ machine2:
我使用 dc.js 和 crossfilter 创建了一个折线图。当前图表如下所示: 必需:我希望左上角的事件非事件图例位于中心图表的下方(底部),并且 x 轴刻度标签应该从一月到十二月开始。我在下面
我仍在尝试使用 dc 和 crossfilter 将图表链接在一起,这里我将表格和饼图链接在一起。链接在图表和表格之间起作用,因此当我单击饼图时,表格会相应更新。 然而,当我点击超链接时,这一行似乎没
我对允许打印填充填充对象的修复有疑问... 所以,完整的故事是我们使用了 Windows GDI FloodFill 函数,我们注意到它在打印机上不起作用,所以我在 inet 上找到的是创建一个内存
我是 MFC 编程的新手。这些天我正在使用 Jeff Prosise 的书来学习 MFC 编程。我遇到过:CClientDC dc (this); 我从 CClientDC 类的定义中找到了一个构造函
我是一名优秀的程序员,十分优秀!