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. ESP8266 + mg90s(舵机)

    ESP8266+mg90s(舵机) 准备阶段 ESP8266(nodeMcu) MG90S(舵机) 面包板 线3 连线 ESP8266 MG90S(舵机) GND 棕色 VCC 红色 模拟引脚 橙色 ...

  2. 构建人工智能模型基础:TFDS和Keras的完美搭配

    上一篇:<数据工程师,转型人工智能岗位的理想时空通道> 序言:本节将带您深入探索 TensorFlow 提供的关键工具和方法,涵盖数据集管理和神经网络模型的构建与训练.在现代人工智能框架中 ...

  3. Nuxt.js 应用中的 webpack:error 事件钩子

    title: Nuxt.js 应用中的 webpack:error 事件钩子 date: 2024/11/25 updated: 2024/11/25 author: cmdragon excerpt ...

  4. 基于.NetCore开发博客项目 StarBlog - (32) 第一期完结

    前言 本文记录一下 StarBlog 项目的当前状态与接下来 v2 版本的开发规划. StarBlog 项目从 2022 年开始至今已经 2 年多了,本来早就该给第一期做个小结的,但这种博客类型的项目 ...

  5. 打包jar 程序,同时将依赖报一起打包

    1.概述 有些时候,我们打包一个java 程序,我需要将所有的依赖也一起打包,这样执行起来方便. 2.我们可以使用maven 插件实现 <build> <plugins> &l ...

  6. OpenTelemetry 101:面向 IT 领导者和爱好者的非技术指南

    如果您从事软件开发. SRE或DevOps工作,您可能听说过可观察性.遥测和跟踪等术语.这些概念对于理解应用程序在生产环境中的行为至关重要,并且它们是现代软件开发实践的重要组成部分. 您可能还听说过在 ...

  7. vue 创建一个项目

    1.npm init -y 2.vue create 名称  3.manuall select features   [手动配置v2,v3] 4.选版本 5 6 启动:npm run serve

  8. 【原创】ARM64 实时linux操作系xenomai4(EVL)构建安装简述

    目录 0 环境说明 1 内核构建 2 库编译 方式1 交叉编译 方式2 本地编译 3 测试 单元测试 hectic:EVL 上下文切换 latmus:latency测试 4 RK3588 xenoma ...

  9. Centos7.8安装Gitlab

    公司为了合规性考虑,需要自己搭建私有化版的github.那不用想,肯定要上GitLab了. 项目背景: 服务器:华为云ECS,需要上公网,并在安全组打开80端口访问. 用户:关闭公开注册,新建用户后, ...

  10. rabbitmq3.7.3 发布了一个新的 exchange x-random

    direct exchange 同一个 routing key 可以绑定多个 queue,当给这个routing key发消息时,所有 queue 都会投递.这个行为对于一些场景不适用,有时我们希望只 ...