- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在将图像加载到 BitmapImage 中并将其转换为 PixelColor 的数组。我想要的是能够操纵这些像素中的一些,使它们更亮/更暗/透明,但我似乎无法让它工作。将其设置为某种颜色确实有效,但 alpha channel 会被忽略。
这是一个使用 C#4 的 WPF 应用
谢谢!
还有代码……
namespace BitmapTest {
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Image = System.Windows.Controls.Image;
public partial class MainWindow {
public MainWindow() {
InitializeComponent();
LoadImage();
}
[DllImport("gdi32")]
private static extern int DeleteObject(IntPtr o);
public static BitmapSource LoadBitmap(Bitmap source) {
var ip = source.GetHbitmap();
try {
return Imaging.CreateBitmapSourceFromHBitmap(ip,
IntPtr.Zero, Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
} finally {
DeleteObject(ip);
}
}
private void LoadImage() {
var i = new Image();
var src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri("road.jpg", UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
var pixels = GetPixels(src);
for (var x = 0; x < Math.Min(50, pixels.GetLength(0)); x++) {
for (var y = 0; y < Math.Min(50, pixels.GetLength(1)); y++) {
pixels[x, y] = new PixelColor {Alpha = 100, Red = pixels[x, y].Red, Green = pixels[x, y].Green, Blue = pixels[x, y].Blue};
}
}
var bitmap = new WriteableBitmap(src.PixelWidth, src.PixelHeight, src.DpiX, src.DpiY, src.Format, src.Palette);
PutPixels(bitmap, pixels, 0, 0);
i.Source = bitmap;
i.Stretch = Stretch.Fill;
RootGrid.Children.Add(i);
}
public PixelColor[,] GetPixels(BitmapSource source) {
if (source.Format != PixelFormats.Bgra32)
source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);
var pixels = source.CopyPixels();
return pixels;
}
public void PutPixels(WriteableBitmap bitmap, PixelColor[,] pixels, int x, int y) {
var width = pixels.GetLength(0);
var height = pixels.GetLength(1);
var sourceRect = new Int32Rect(0, 0, width, height);
bitmap.WritePixels(sourceRect, pixels, width*4, x, y);
}
[StructLayout(LayoutKind.Sequential)]
public struct PixelColor {
public byte Blue;
public byte Green;
public byte Red;
public byte Alpha;
}
}
public static class BitmapSourceHelper {
public static MainWindow.PixelColor[,] CopyPixels(this BitmapSource source) {
if (source.Format != PixelFormats.Bgra32)
source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);
var pixels = new MainWindow.PixelColor[source.PixelWidth, source.PixelHeight];
var stride = source.PixelWidth * ((source.Format.BitsPerPixel + 7) / 8);
var pinnedPixels = GCHandle.Alloc(pixels, GCHandleType.Pinned);
source.CopyPixels(
new Int32Rect(0, 0, source.PixelWidth, source.PixelHeight),
pinnedPixels.AddrOfPinnedObject(),
pixels.GetLength(0) * pixels.GetLength(1) * 4,
stride);
pinnedPixels.Free();
return pixels;
}
}
}
最佳答案
这是在 Windows 窗体应用程序中使图像变亮和变暗的示例。此处的代码基于代码项目网站上一篇文章的亮度过滤器部分:Image Processing for Dummies with C# and GDI+ Part 1 .
public Bitmap Lighten(Bitmap bitmap, int amount)
{
if (amount < -255 || amount > 255)
return bitmap;
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
int nVal = 0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
int nOffset = stride - bitmap.Width * 3;
int nWidth = bitmap.Width * 3;
for (int y = 0; y < bitmap.Height; ++y)
{
for (int x = 0; x < nWidth; ++x)
{
nVal = (int)(p[0] + amount);
if (nVal < 0) nVal = 0;
if (nVal > 255) nVal = 255;
p[0] = (byte)nVal;
++p;
}
p += nOffset;
}
}
bitmap.UnlockBits(bmData);
return bitmap;
}
private void btnLighten_Click(object sender, EventArgs e)
{
Bitmap image = pictureBox1.Image as Bitmap;
pictureBox1.Image = Lighten(image, 10);
}
private void btnDarken_Click(object sender, EventArgs e)
{
Bitmap image = pictureBox1.Image as Bitmap;
pictureBox1.Image = Lighten(image, -10);
}
关于c# - 使图像变亮/变暗/透明部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7657922/
使用 Vanilla JS,我希望执行如下图所示的操作,其中除了特定部分(最好是椭圆形)之外,大部分 Canvas 都变暗。 我可以通过在前面绘制半透明颜色来轻松使 Canvas 变暗,但我不确定如何
我想将 UIView “变暗”一定程度。现在,我正在像这样进行变暗: UIView *overlay = [[UIView alloc] initWithFrame:mainView.bounds];
我有一个 TCard(TGraphicControl 组件)并且它有一个属性背景(TP图片) 我希望能够使背景变暗或变暗。因此,如果我可以在游戏中玩卡片,那么它是正常的。如果我不能在游戏中玩这张卡,那
如果您熟悉 iTerm2 应用程序,您就会知道您可以像 vim 一样 Split View,并且不活动的 View 会“变暗”。 我通常在 vim 中使用三个垂直 Split View,例如,通过将背
我正在尝试创建一些 jQuery 函数,将对象 bgcolor 更改为更亮或更暗(您将参数设置为色调差异)。我认为它应该可以工作,但是它破裂了。 $.fn.changeBg = function(di
我有两个 fragment ,其中一个具有完整 View 的大小。另一个 fragment 较小并放置在其上方。如何使第一个 fragment 变暗? 最佳答案 在 fragment XML 文件中,
我在一个 Pane 中使用 irssi 和 nicklist.pl在另一个中,每当我在 irssi Pane 中时,昵称列表 Pane 就会变暗: 如何阻止非事件 Pane 变暗? 最佳答案 找到了!
我试图做的是使 UIImageView 更暗,以便图像结果顶部的按钮和 View 。我想给它一个在 ios6 中用户使用 Facebook 分享时给出的效果。它后面的所有内容都变得更暗,但它是可见的。
在单击登录按钮后出现的进度条微调器中,我希望当微调器在屏幕上可见时微调器后面的元素变暗或变暗。我的布局文件目前看起来像这样:
在我的应用程序中,我有一个带有进度条的刷新功能。进度条位于应该刷新的 TextView 上。问题是:如果 textview 中有很多文本,你就看不到进度条了。是否可以在 onPreExcute/onP
操作表自然会使背景 View 稍微变灰,但并不明显。我想让它变得更暗。 还有,我要怎么做才能让您无法点击其他地方?现在,如果您在操作表外部单击,操作表就会消失。我希望这种情况不会发生 - 您必须按下“
我想创建一个主页,其背景图像填充整个页面高度。在此之上,我需要添加白色文本和其他元素。 This website是我想要实现的一个例子。 我想要的: 外观: 使用下面的 CSS,文本看起来非常模糊。
我一直在开发一款增强现实游戏,并且正在实现我的暂停菜单。我的暂停菜单完全可用,UIView 显示为 ARSKView 之上的另一层。但是,我想在显示暂停菜单时使 ARSKView 变暗,然后在关闭暂停
我在 View 的右上角有一个菜单按钮 (+),当按下按钮时,其他几个按钮会下拉供用户选择。 我想有效地调暗所有这些按钮下方的所有 View 。 我目前的做法是在所有内容之上设置一个透明 View ,
我是网络开发的半菜鸟,但我不是在使用错误的术语进行搜索,就是我不是一个非常熟练的 googler。 我正在制作一个单页网站,并且想要这样的效果,即当向下滚动新部分进入 View 时,新部分会变暗。我可
我的应用程序的背景是一个不透明的 UIImageView。在某些情况下,我想以动画方式将它从全亮度调暗到大约 50%。目前我降低了 View 的 alpha 属性并且效果很好。因为 View 后面没有
我需要向用户显示 alert 并且我希望它很漂亮。所以我使用 Bootstrap 的警报类,并像这样向用户显示 div: Some text 这段代码只显示了一个div。我想要的是将所有东
我需要在触摸 UIImageView 时使它变暗, 几乎就像跳板(主屏幕)上的图标一样。 我应该添加具有 0.5 alpha 和黑色背景的 UIView 吗?这看起来很笨拙。我应该使用图层还是其他东西
我正在将图像加载到 BitmapImage 中并将其转换为 PixelColor 的数组。我想要的是能够操纵这些像素中的一些,使它们更亮/更暗/透明,但我似乎无法让它工作。将其设置为某种颜色确实有效,
我找到了 change the opacity of a View 的方法,但我实际上需要使 View 变暗。我最好的办法是在上面放一个透明的黑色矩形,然后慢慢增加矩形的不透明度。 你知道更好的方法吗
我是一名优秀的程序员,十分优秀!