- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想在拖放操作中使用部分透明的图像。这一切都已设置好并且工作正常,但实际转换为透明有一个奇怪的副作用。出于某种原因,像素似乎与黑色背景混合在一起。
下图描述了问题:
图a)是原始位图。
图 b) 是执行 alpha 混合后的结果。显然,这比预期的 50% alpha 过滤器要暗很多。
图 c) 是所需的效果,图 a) 具有 50% 的透明度(使用绘图程序添加到合成中)。
我用来生成透明图像的代码如下:
Bitmap bmpNew = new Bitmap(bmpOriginal.Width, bmpOriginal.Height);
Graphics g = Graphics.FromImage(bmpNew);
// Making the bitmap 50% transparent:
float[][] ptsArray ={
new float[] {1, 0, 0, 0, 0}, // Red
new float[] {0, 1, 0, 0, 0}, // Green
new float[] {0, 0, 1, 0, 0}, // Blue
new float[] {0, 0, 0, 0.5f, 0}, // Alpha
new float[] {0, 0, 0, 0, 1} // Brightness
};
ColorMatrix clrMatrix = new ColorMatrix(ptsArray);
ImageAttributes imgAttributes = new ImageAttributes();
imgAttributes.SetColorMatrix(clrMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(bmpOriginal, new Rectangle(0, 0, bmpOriginal.Width, bmpOriginal.Height), 0, 0, bmpOriginal.Width, bmpOriginal.Height, GraphicsUnit.Pixel, imgAttributes);
Cursors.Default.Draw(g, new Rectangle(bmpOriginal.Width / 2 - 8, bmpOriginal.Height / 2 - 8, 32, 32));
g.Dispose();
imgAttributes.Dispose();
return bmpNew;
有谁知道为什么 alpha 混合不起作用?
更新一:
为清楚起见,如果我在绘制的表面上进行 alphablending,代码确实有效。问题是我想从现有图像创建一个完全半透明的图像,并在拖/放操作期间将其用作动态光标。即使跳过上面的步骤,只绘制一个颜色为 88ffffff 的填充矩形,也会产生深灰色。图标有问题。
更新二:
因为我已经研究了很多并且相信这与 Cursor 的创建有关,所以我也将在下面包含该代码。如果我在 CreateIconIndirect 调用之前对位图进行 GetPixel 采样,则四个颜色值似乎完好无损。因此我觉得罪魁祸首可能是 IconInfo 结构的 hbmColor 或 hbmMask 成员。
这是 IconInfo 结构:
public struct IconInfo { // http://msdn.microsoft.com/en-us/library/ms648052(VS.85).aspx
public bool fIcon; // Icon or cursor. True = Icon, False = Cursor
public int xHotspot;
public int yHotspot;
public IntPtr hbmMask; // Specifies the icon bitmask bitmap. If this structure defines a black and white icon,
// this bitmask is formatted so that the upper half is the icon AND bitmask and the lower
// half is the icon XOR bitmask. Under this condition, the height should be an even multiple of two.
// If this structure defines a color icon, this mask only defines the AND bitmask of the icon.
public IntPtr hbmColor; // Handle to the icon color bitmap. This member can be optional if this structure defines a black
// and white icon. The AND bitmask of hbmMask is applied with the SRCAND flag to the destination;
// subsequently, the color bitmap is applied (using XOR) to the destination by using the SRCINVERT flag.
}
下面是实际创建 Cursor 的代码:
public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot) {
IconInfo iconInfo = new IconInfo();
GetIconInfo(bmp.GetHicon(), ref iconInfo);
iconInfo.hbmColor = (IntPtr)0;
iconInfo.hbmMask = bmp.GetHbitmap();
iconInfo.xHotspot = xHotSpot;
iconInfo.yHotspot = yHotSpot;
iconInfo.fIcon = false;
return new Cursor(CreateIconIndirect(ref iconInfo));
}
两个外部函数定义如下:
[DllImport("user32.dll", EntryPoint = "CreateIconIndirect")]
public static extern IntPtr CreateIconIndirect(ref IconInfo icon);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
最佳答案
在与 GDI(和 Win32)进行互操作时,GDI+ 有许多与 alpha 混合相关的问题。在这种情况下,调用 bmp.GetHbitmap() 会将您的图像与黑色背景混合。一个article on CodeProject提供了有关该问题的更多详细信息,以及用于将图像添加到图像列表的解决方案。
您应该能够使用类似的代码来获取用于掩码的 HBITMAP:
[DllImport("kernel32.dll")]
public static extern bool RtlMoveMemory(IntPtr dest, IntPtr source, int dwcount);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateDIBSection(IntPtr hdc, [In, MarshalAs(UnmanagedType.LPStruct)]BITMAPINFO pbmi, uint iUsage, out IntPtr ppvBits, IntPtr hSection, uint dwOffset);
public static IntPtr GetBlendedHBitmap(Bitmap bitmap)
{
BITMAPINFO bitmapInfo = new BITMAPINFO();
bitmapInfo.biSize = 40;
bitmapInfo.biBitCount = 32;
bitmapInfo.biPlanes = 1;
bitmapInfo.biWidth = bitmap.Width;
bitmapInfo.biHeight = -bitmap.Height;
IntPtr pixelData;
IntPtr hBitmap = CreateDIBSection(
IntPtr.Zero, bitmapInfo, 0, out pixelData, IntPtr.Zero, 0);
Rectangle bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
BitmapData bitmapData = bitmap.LockBits(
bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb );
RtlMoveMemory(
pixelData, bitmapData.Scan0, bitmap.Height * bitmapData.Stride);
bitmap.UnlockBits(bitmapData);
return hBitmap;
}
关于c# - Windows 窗体 : Making a cursor bitmap partially transparent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1236473/
我的 node.js 应用程序中有一个 Handlebars / mustache 布局文件,如下所示: {{> header}} {{> navbar}} {{{body}}} {{> footer
我的 node.js 应用程序中有一个 Handlebars / mustache 布局文件,如下所示: {{> header}} {{> navbar}} {{{body}}} {{> footer
我有一个主文件,home.html.erb在里面我渲染了大约 15 个部分。这些部分非常小,我想知道是否可以将它们全部放在一个文件中,然后部分地呈现部分部分。 例如,我会制作 _big_partial
在下面的 TypeScript 代码片段中,我需要从一个对象分配给另一个对象,其中它们都是 Partial .在这里,我的直觉是 typescript 应该能够理解正在发生的事情,因为在第 (B) 行
在我的应用程序中,用户通过 AJAX 呈现 _show.html.erb 部分。我现在想要的是在该部分中有一个按钮,单击时将其关闭。 我遇到的问题:如果我将按钮放在部分之外,一切都会正常 - 我可以
我有人造 curry Programming Clojure书。 user=> (defn faux-curry [& args] (apply partial partial args)) #'us
我在尝试实现 AJAX 时遇到了一些困难,其中“link_to” View 被渲染,并且在该 View 中我有另一个 AJAX 调用。 我有菜单侧边栏,其中有这样的内容: "> 在applicatio
我是 asp.net MVC 的新手,请告诉我应该在何处使用局部 View 以及在何处渲染局部 View 。提前致谢 最佳答案 This link might help. Html.RenderPar
我在下拉菜单的 Onchange 事件上更新 DIV 元素。而我正在使用partial 来替换DIV 中的内容。这是我的 ajax 调用: var cach_this = this;
我正在使用 UI-Router AngularJS 的框架,以呈现嵌套的部分。我在渲染父部分及其子部分时遇到问题。这是我的代码: window.app.config(['$stateProvider'
是否存在包含可从Partial中访问的Partial名称的变量?。在_foo.haml中:
我有/views/layouts/_navigation.html.erb,其中生成了部分用户配置文件:
我有一组我想用部分渲染的项目: @items = ['a','b','c'] @items, :partial => 'item' %> 我想用升序对元素进行编号。所以输出应该是: 3: a 2:
尝试使用 .Netcore 制作 Web 应用程序 当我运行该应用程序时,出现此错误。帮我 这不是错误而是警告。但帮我解决 我在下面添加了我的代码 @ViewBag.Title
标题有点令人困惑。 我正在尝试实现一些类似 reddit 的评论系统。这样您就可以查看 Post 并向其添加多态的 Comment 。或者,评论另一条评论。 我的观点是这样的: Post:
我需要编写一个算法来引导机器人穿过“迷宫”(一个有起点、目标、空白区域和不可穿越的空间或“墙壁”的矩形网格)。它可以在任何基本方向(N、NW、W、SW、S、SE、E、NE)上移动,每次移动的成本不变。
?sort指出partial参数可以是NULL或用于部分排序的索引向量。 我试过了: x <- c(1,3,5,2,4,6,7,9,8,10) sort(x) ## [1] 1 2 3 4
MVC4,单击下拉项时,JavaScript 函数在 View 的“脚本”部分中调用。函数对 Controller Action 进行ajax调用,返回Json数据。我需要将一些返回值传递给 Html
我正在使用 Automapper 将数据从 objectA 传输到 objectB classe ObjectA { string Title; string Summary; } cla
我想使用 TortoiseSVN 提交文件的一部分,有什么方法可以做到这一点吗? 我将举一个例子来更清楚地说明我为什么要这样做。 我有一个文件,其中包含一些在构建过程中被替换的定义,如下所示: #de
我是一名优秀的程序员,十分优秀!