WPF BitmapSource /BitmapImage 获取像素点颜色
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.ComponentModel; using System.Drawing.Imaging;
using System.Windows.Media.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Media;
using System.Windows; namespace WpfMosaic
{
[StructLayout(LayoutKind.Sequential)]
public struct PixelColor
{
public byte Blue;
public byte Green;
public byte Red;
public byte Alpha;
} public class Utils
{
public static PixelColor[,] GetPixels(BitmapSource source)
{
if (source.Format != PixelFormats.Bgra32)
source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);
PixelColor[,] pixels = new PixelColor[source.PixelWidth, source.PixelHeight];
int stride = source.PixelWidth * ((source.Format.BitsPerPixel + 7) / 8);
GCHandle 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;
}
}
}
设置颜色后保存图片:
public static Bitmap setColor(PixelColor[,] pixel) {
Bitmap bmp = new Bitmap(1080,1920);
// the target colors
// var palette = new BitmapPalette(new List<System.Windows.Media.Color> { Colors.Yellow, Colors.Red });
int k = 0;
for (int i = 0;i< 1080; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
bmp.SetPixel(i, j, System.Drawing.Color.FromArgb(pixel[i,j].Alpha, pixel[i, j].Red, pixel[i, j].Green, pixel[i, j].Blue));
k++;
}
}
bmp.Save(@"C:\Users\gwang\Pictures\0011122.jpg", ImageFormat.Jpeg);
return bmp;
}
// 用法:
//var pixels = GetPixels(image);
//if(pixels[7, 3].Red > 4)
上面发现颜色不对。。。。。。。。。。。。。
下面的发现 颜色变黄了:
public static PixelColor[,] GetPixels2(BitmapSource source)
{
PixelColor[,] result= new PixelColor[source.PixelWidth, source.PixelHeight]; int stride = source.PixelWidth * 4;
int size = source.PixelHeight * stride;
byte[] pixels = new byte[size];
source.CopyPixels(pixels, stride, 0); for (int y = 0; y < source.PixelHeight; y++)
{
for (int x = 0; x < source.PixelWidth; x++)
{ int index = y * stride + 4 * x;
byte red = pixels[index];
byte green = pixels[index + 1];
byte blue = pixels[index + 2];
byte alpha = pixels[index + 3];
result[x, y] = new PixelColor()
{
Alpha = alpha,
Red = red,
Green = green,
Blue = blue
}; } } return result; }
蛋疼。。。。。。。。。。。。。。。。。。。。
WPF BitmapSource /BitmapImage 获取像素点颜色的更多相关文章
- WPF 精修篇 获取系统颜色和字体样式
原文:WPF 精修篇 获取系统颜色和字体样式 看效果 <Grid> <Rectangle Fill="{DynamicResource {x:Static SystemCo ...
- ios像素点颜色取样
一.像素点颜色取样 + (UIColor*) getPixelColorAtLocation:(CGPoint)point inImage:(UIImage *)image { UIColor* co ...
- [ActionScript 3.0] AS3.0 获取像素点的灰度
/** * 获取像素点的灰度 * @color 像素点的颜色值 * @return uint */ function getGray(color:uint):uint { return getR(co ...
- 获取随机颜色js
获取随机颜色方法一: function randomColor1() { var rand = Math.floor(Math.random() * 0xFFFFFF).toString(16); i ...
- js获取背景颜色
//js获取背景颜色var Airport=$("#Airport").css('background-color'); js设置背景颜色 $("#intercity&q ...
- WPF编程,获取句柄将外部程序嵌入到WPF界面。
原文:WPF编程,获取句柄将外部程序嵌入到WPF界面. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/details ...
- VS编程,WPF中,获取鼠标相对于当前屏幕坐标的一种方法
原文:VS编程,WPF中,获取鼠标相对于当前屏幕坐标的一种方法 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/det ...
- VS编程,WPF中,获取鼠标相对于当前程序窗口的坐标的一种方法
原文:VS编程,WPF中,获取鼠标相对于当前程序窗口的坐标的一种方法 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/ ...
- [Winform][C#]获取系统颜色预定义颜色和现有字体集
转自: http://zhidao.baidu.com/link?url=ozY7tJRNBYHUsImE6jn1psqc8owib7MWcDMEmZw48q8iD9Hz9MWgnQQcBDO0VYO ...
- WPF中取得预定义颜色
原文:WPF中取得预定义颜色 使用XAML代码取得.net预定义颜色:<Page xmlns="http://schemas.microsoft.com/winfx/2006/x ...
随机推荐
- Uniswap V2 核心 合约代码
Uniswap V2 核心 UniswapV2Factory UniswapV2Pair UniswapV2ERC20 IUniswapV2Router02 1. UniswapV2Factory 合 ...
- TOYOTA SYSTEMS Programming Contest 2024(AtCoder Beginner Contest 377) 补题记录(A-E)
AtCoder Beginner Contest 377 A - Rearranging ABC 字符串有ABC三个字母即可. #include<bits/stdc++.h> using ...
- 2款使用.NET开发的数据库系统
前言 今天大姚给大家分享2款使用.NET开发且开源的数据库系统. Garnet Garnet是一款由微软研究院基于.NET开源的高性能.跨平台的分布式缓存存储数据库,该项目提供强大的性能(吞吐量和延迟 ...
- Xshell终端连接服务器慢,问题解决方法
Xshell连接慢的话,先在xshell中设置中点隧道然后把转发xshell的勾取消 /etc/ssh/sshd_config 里边修改第115行UseDNS把注释取消然后yes改为no 然后重启ss ...
- 关于CP2102(USB转TTL模块)的驱动安装、电脑识别、接线
一.CP2102 与 CH340 对比 特性 CP2102 CH340 芯片制造商 Silicon Labs WCH(江苏沁恒) 接口 USB USB 最大波特率 1 Mbps 2 Mbps 支持操作 ...
- AI千恋万花(java调用api实现)附完整项目及注释)重置版)
感觉博客的第一版质量有点低下了,删了重置一下,希望能给其他人的代码带来一些灵感 前情提要:https://www.cnblogs.com/h4o3/p/18523151 由于是匆忙制作的老婆系统,主界 ...
- 拯救php性能的神器webman-使用后台webman-admin
在webman的插件市场里面发现了这个 webman-admin 安装的话很简单,就是在已经安装了 webman 的目录里面执行 composer require -W webman/admin 安 ...
- 一键生成美观的彩页演示+AI的训练过程科普
一键生成美观彩页 + AI训练揭秘:让你的内容瞬间高大上! 阅读时间: 8分钟 | 字数: 1300+ 你是否曾为制作精美的演示文稿而烦恼?是否对AI的训练过程充满好奇?今天,让我们一起探索如何用AI ...
- 记录一次CTF经典PHP反序列化
PHP反序列化 序列化通俗来讲就是将对象转化为可以存储.传输的字符串,反序列化就是把字符串再变回对象的过程. 例如: <?php class chybate { var $test = '123 ...
- linux bash shell 入门教程()
Shell Script(bash)简介 众所皆知地,UNIX上以小工具著名,利用许多简单的小工具,来完成原本需要大量软体开发的工作,这一点特色,使得UNIX成为许多人心目中理想的系统平台. 在众多的 ...