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 获取像素点颜色的更多相关文章

  1. WPF 精修篇 获取系统颜色和字体样式

    原文:WPF 精修篇 获取系统颜色和字体样式 看效果 <Grid> <Rectangle Fill="{DynamicResource {x:Static SystemCo ...

  2. ios像素点颜色取样

    一.像素点颜色取样 + (UIColor*) getPixelColorAtLocation:(CGPoint)point inImage:(UIImage *)image { UIColor* co ...

  3. [ActionScript 3.0] AS3.0 获取像素点的灰度

    /** * 获取像素点的灰度 * @color 像素点的颜色值 * @return uint */ function getGray(color:uint):uint { return getR(co ...

  4. 获取随机颜色js

    获取随机颜色方法一: function randomColor1() { var rand = Math.floor(Math.random() * 0xFFFFFF).toString(16); i ...

  5. js获取背景颜色

    //js获取背景颜色var Airport=$("#Airport").css('background-color'); js设置背景颜色 $("#intercity&q ...

  6. WPF编程,获取句柄将外部程序嵌入到WPF界面。

    原文:WPF编程,获取句柄将外部程序嵌入到WPF界面. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/details ...

  7. VS编程,WPF中,获取鼠标相对于当前屏幕坐标的一种方法

    原文:VS编程,WPF中,获取鼠标相对于当前屏幕坐标的一种方法 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/det ...

  8. VS编程,WPF中,获取鼠标相对于当前程序窗口的坐标的一种方法

    原文:VS编程,WPF中,获取鼠标相对于当前程序窗口的坐标的一种方法 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/ ...

  9. [Winform][C#]获取系统颜色预定义颜色和现有字体集

    转自: http://zhidao.baidu.com/link?url=ozY7tJRNBYHUsImE6jn1psqc8owib7MWcDMEmZw48q8iD9Hz9MWgnQQcBDO0VYO ...

  10. WPF中取得预定义颜色

    原文:WPF中取得预定义颜色 使用XAML代码取得.net预定义颜色:<Page    xmlns="http://schemas.microsoft.com/winfx/2006/x ...

随机推荐

  1. SSIS连接Excel2007版本之后的数据源

    今天我发现了新大陆,兴奋得不得了,由于原文写得太过详细与专业,我就偷偷懒直接Copy过来了,怕自己以后没地儿找,哈哈哈 原文链接:https://www.cnblogs.com/biwork/p/34 ...

  2. 从使用delete释放指针导致程序崩溃看变量初始化

    先来看下面的代码 bool FuncTest(LPCTSTR lpcProc) { bool bRet = false; ... if (CONDITION1) { goto FUNC_CLEAN; ...

  3. 终于找到了英特尔CPU缩缸的原因!如何自救?

    地址: https://www.youtube.com/watch?v=D0wOiillq_A

  4. 流程编排LiteFlow-业务代码解耦

    LiteFlow真的是相见恨晚啊,之前做过的很多系统,都会用各种if else,switch这些来解决不同业务方提出的问题,有时候还要"切一个分支"来搞这些额外的事情,把代码搞得一 ...

  5. 鲜花:bitset求解高维偏序

    书接上回 一维偏序直接做.二维偏序套线段树或归并排序.三维偏序可以树套树或者 CDQ 套树,那四维偏序呢?可以 CDQ 套树套树.那五维偏序呢?可以发现,无论是 CDQ 分治还是树,都很难再继续嵌套, ...

  6. MinIO Linux 安装使用 & SpringBoot整合MinIO

    目录 MinIO Linux 安装 单节点部署 创建 systemd 系统启动服务文件 创建环境变量文件 启动MinIO服务 连接到MinIO服务 SpringBoot项目整合MinIO 配置项 工具 ...

  7. An Entry Example of Log4j

    The log4j can be configured both programmatically and externally using special configuration files. ...

  8. Django之跨域

    解决跨域请求问题可以从前端解决也可以通过配置后台解决,通过配置后台允许跨域可以解决前端的一些麻烦.Django通过中间件实现允许跨域. 1.安装django-cors-headers中间件 pip i ...

  9. 阿里巴巴LangEngine开源了!支撑亿级网关规模的高可用Java原生AI应用开发框架

    LangEngine作为阿里集团内部发起的纯Java版本的AI应用开发框架,经过充分实践,已经广泛应用于包括淘宝.天猫.阿里云.爱橙科技.菜鸟.蚂蚁.飞猪.1688.LAZADA等在内的多个业务场景. ...

  10. GPUStack v0.4:文生图模型、语音模型、推理引擎版本管理、离线支持和部署本地模型

    GPUStack 是一个专为运行 AI 模型设计的开源 GPU 集群管理器,致力于支持基于任何品牌的异构 GPU 构建统一管理的算力集群.无论这些 GPU 运行在 Apple Mac.Windows ...