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 ...
随机推荐
- Centos7下Docker搭建Matomo
1.docker安装和启动 wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repo ...
- .NET 平台 WPF 通用权限开发框架 (ABP)
前言 对于大多数.NET后端开发者而言,ABP框架已经相当熟悉,可以轻松进行二次开发,无需重复实现用户角色管理.权限控制.组织管理和多租户等功能. 然而,ABP框架主要专注于Web应用,对于桌面端和移 ...
- ABP - 菜单配置(导航栏选中高亮,高亮并定位当前标题)
配置一个如上图所示的菜单: 1.打开文件NavigationProvider.cs 添加如下代码(如下图所示) .AddItem(new MenuItemDefinition( PageNames.A ...
- 一文讲透 FPGA CDC 多bit跨时钟域同步-hand-shanking机制
一.背景 数据的跨时钟域处理是FPGA开发过程中的常见问题,存在两种情况 慢时钟向快时钟同步:只需在快时钟域打两拍即可.其RTL如下: 打拍同步的原理:大家在初学FPGA时,经常听过FPGA中对信号打 ...
- Go语言Context包源码学习
0前言 context包作为使用go进行server端开发的重要工具,其源码只有791行,不包含注释的话预计在500行左右,非常值得我们去深入探讨学习,于是在本篇笔记中我们一起来观察源码的实现,知其然 ...
- 关于switch的优化:转移表(完结)
用转移表来代替功能实现中的 switch 语句 1. switch 语句应用场景 在许多场景中会用到条件判断,此时简单的条件判断一般通过 if/else 语句实现:如果涉及三条以上的功能分支一般会通过 ...
- 成为JavaGC专家Part I — 深入浅出Java垃圾回收机制
对于Java开发人员来说,了解垃圾回收机制(GC)有哪些好处呢?首先可以满足作为一名软件工程师的求知欲,其次,深入了解GC如何工作可以帮你写出更好的Java应用. 这仅仅代表我个人的意见,但我坚信一个 ...
- ZCMU-1133
emm就直接看的前辈的了. 唉 #include <stdio.h> #include <string.h> #include <algorithm> //我不成熟 ...
- 使用ProWindow时,控制按钮状态的说明
在Pro SDK中,提供了一个默认的窗口基类,ProWindow Class,提供了基础的窗体样式,可供扩展和调用. 有网友问我,在使用时,会发现窗体右上角的控制按钮,有时会没有按照自己的预期显示. ...
- vue3 + vite 报错处理
TypeError: vite.defineConfig is not a function 执行命令:npm install @vitejs/plugin-vue -D config里面配置代码: ...