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. Air201资产定位模组LuatOS:录音&播放录音功能的操作方法

    ​ 一直有小伙伴们问,迷你小巧的合宙Air201虽然有很多优点,超低功耗.精准定位,那么它是否支持录音.播放录音功能? 那必须能!高集成化设计的Air201自带了ES8311音频解码芯片(Audio ...

  2. 【一步步开发AI运动小程序】十三、自定义一个运动分析器,实现计时计数02

    随着人工智能技术的不断发展,阿里体育等IT大厂,推出的"乐动力"."天天跳绳"AI运动APP,让云上运动会.线上运动会.健身打卡.AI体育指导等概念空前火热.那 ...

  3. 拯救php性能的神器webman-打包二进制

    看了看webman的官方文档,发现居然还能打包为二进制,这样太厉害了吧! 先执行这个  composer require webman/console ^1.2.24 安装这个console的包,然后 ...

  4. 崖山数据库-监控运维平台-YCM 配置部署详解

    准备工作:操作系统版本:[root@node10 ~]# uname -aLinux node10 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 ...

  5. Docker之基础(一)

    接触Docker有很久一段时间, 但是没有好好总结一下, 借此公司项目全面容器化, 记录一下常用的Docker操作 概况: 本次容器化的项目包括PHP+Python项目,PHP是基于php-fpm的基 ...

  6. linux之Zip

    安装: apt-get) apt-get install zip yum) yum install -y unzip zip 语法: zip [选项] 压缩包名 源文件或源目录列表 注意,zip 压缩 ...

  7. vim之常用插件

    Vundle是vim的一个插件管理器, 同时它本身也是vim的一个插件.插件管理器用于方便.快速的安装.删除.Vim更新插件.vim Vundle插件官方地址:https://github.com/V ...

  8. 升级Linux内核版本

    ```shell# 查看内核版本,jw版本ceph默认format=2, 2.x 及之前的的内核版本需手动调整format=1# 4.x之前要关闭object-map fast-diff deep-f ...

  9. Nuxt.js 应用中的 request 事件钩子

    title: Nuxt.js 应用中的 request 事件钩子 date: 2024/12/4 updated: 2024/12/4 author: cmdragon excerpt: 在构建现代 ...

  10. 全球气象数据ERA5的下载方法

      本文介绍在ERA5气象数据的官方网站中,手动下载.Python代码自动批量下载逐小时.逐日与逐月的ERA5气象数据各类产品的快捷方法.   ERA5(fifth generation ECMWF ...