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. java可变参数案例学习

    /* ** 可变参数案例 */ public class changeVar { public static void main(String[] args) { printMax(34, 3, 2, ...

  2. Go语言切片(Slice)的一些有趣特性

    切片类似数组的引用.更改底层数组中的元素会修改切片的元素.更改切片的元素同样会修改其底层数组中的元素,和它共享底层数组的切片都会观测到这些修改. 点击查看代码 package main import ...

  3. shell脚本安装卸载统一脚本

    #!/bin/bash set -e OUT_DIR=out function usage() { cat - <<-EOF SlightShift-SPB Kit Usage: $0 & ...

  4. html代码新手教学

    HTML 是超文本标记语言(HyperText Markup Language)的缩写,是用来描述网页结构的标记语言.在这篇教学中,我们将介绍一些 HTML 基础知识,帮助新手快速学习并掌握如何编写简 ...

  5. 万字长文带你深入Redis底层数据结构

    Redis数据库的数据结构 Redis 的键值对中的 key 就是字符串对象,而 value 就是指Redis的数据类型,可以是String,也可以是List.Hash.Set. Zset 的数据类型 ...

  6. (Python基础教程之十八)Python字典交集–比较两个字典

    Python示例,用于查找2个或更多词典之间的常见项目,即字典相交项目. 1.使用"&"运算符的字典交集 最简单的方法是查找键,值或项的交集,即 & 在两个字典之间 ...

  7. JVM 核心技术

    1.Java代码编译和执行的整个过程: 2. JVM内存管理及垃圾回收机制:  先看图, 二.Java代码编译和执行的整个过程 Java代码编译是由Java源码编译器来完成,流程图如下所示: Java ...

  8. Java 并发编程实战学习笔记——寻找可强化的并行性

    寻找可强化的并行性 1.串行执行所有任务 代码 复制 - 运行 package net.jcip.examples; import java.util.*; /** * SingleThreadRen ...

  9. 2019-2020 ACM-ICPC Brazil Subregional Programming Contest

    D. Denouncing Mafia 给定一颗树,然后给定\(k\)个起点,对于每个起点来说,从该点到根节点的一条链都会被染色,求最多有几个点会被染色 \(3 \leq n \leq 1e5, 1 ...

  10. vue项目中如何加载markdown

    场景 今天忽然临时接到一个需求: 就是将markdown文件直接在vue项目中进行加载,并正常显示出来. 这......,我知道是可以进行加载markdown文件的. 但是我之前没有做过,答复的是:可 ...