将彩色图片变灰色?

ljjwf 2010-12-04 10:41:48
silverlight中点击一个按钮 将彩色图片变灰色 后台代码咋写啊
...全文
311 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tinalovetina 2012-10-19
  • 打赏
  • 举报
回复
WriteableBitmap bitmap = new WriteableBitmap(this.imgSource, null);
for (int y = 0; y < bitmap.PixelHeight; y++)
用到项目中,其中bitmap.PixelHeight=0,为什么?
lcp147572931 2010-12-06
  • 打赏
  • 举报
回复
[Quote=引用楼主 ljjwf 的回复:]
silverlight中点击一个按钮 将彩色图片变灰色 后台代码咋写啊
[/Quote]

<Grid x:Name="LayoutRoot" Background="White">
<Image Height="65" HorizontalAlignment="Left" Margin="69,76,0,0" Name="imgSource" Stretch="Fill" VerticalAlignment="Top" Width="137" Source="test.jpg" />
<Button Content="转为黑白" Height="28" HorizontalAlignment="Left" Margin="217,113,0,0" Name="btnConvertBnW" VerticalAlignment="Top" Width="76" FontSize="14" Click="btnConvertBnW_Click" />
<Image Height="65" HorizontalAlignment="Left" Margin="307,76,0,0" Name="imgTarget" Stretch="Fill" VerticalAlignment="Top" Width="137" />
</Grid>


private void btnConvertBnW_Click(object sender, RoutedEventArgs e)
{
//读取想转换的图片并转为WritableBitmap
WriteableBitmap bitmap = new WriteableBitmap(this.imgSource, null);
for (int y = 0; y < bitmap.PixelHeight; y++)
{
for (int x = 0; x < bitmap.PixelWidth; x++)
{
//获取每一个像素
int pixelLocation = bitmap.PixelWidth * y + x;
int pixel = bitmap.Pixels[pixelLocation];
byte[] pixBytes = BitConverter.GetBytes(pixel);
//每一个都除3
byte bnwPixel = (byte)(.333 * pixBytes[2] + .333 * pixBytes[1] + .333 * pixBytes[0]);
pixBytes[0] = bnwPixel;//b
pixBytes[1] = bnwPixel;//g
pixBytes[2] = bnwPixel;//r
//将处理后的像素返回
bitmap.Pixels[pixelLocation] = BitConverter.ToInt32(pixBytes, 0);
}
}
//显示结果
this.imgTarget.Source = bitmap;
}

夜行空 2010-12-04
  • 打赏
  • 举报
回复
帮顶

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧