博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
位深度1的图片操作
阅读量:5034 次
发布时间:2019-06-12

本文共 4843 字,大约阅读时间需要 16 分钟。

using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Imaging;using System.Runtime.InteropServices;namespace CSammisRun.Imaging{    ///     /// A base class which represents a 1bpp TIFF image    ///     public class OneBppImage    {        ///         /// Initializes a new 1bpp image from a TIFF file        ///         public OneBppImage(string fileName)        {            using (Bitmap bitmap = new Bitmap(fileName))            {                FromBitmap(bitmap);            }        }        ///         /// Initializes a new 1bpp image from a 
///
public OneBppImage(Bitmap bitmap) { FromBitmap(bitmap); } /// /// Copies the specified
into the new instance ///
public OneBppImage(OneBppImage image) { this.Height = image.Height; this.Width = image.Width; this.ImageData = new byte[Width, Height]; for (int x = 0; x < Width; x++) for (int y = 0; y < Height; y++) this.ImageData[x, y] = image.ImageData[x, y]; } /// /// Initializes a new 1bpp image from an array in memory /// public OneBppImage(byte[,] imageData) { this.Width = imageData.GetLength(0); this.Height = imageData.GetLength(1); this.ImageData = imageData; } /// /// Gets the image data as an array of bytes which are either
/// or
///
public byte[,] ImageData { get; private set; } /// /// Gets the width of the image /// public int Width { get; private set; } /// /// Gets the height of the image /// public int Height { get; private set; } #region Bitmap read/write methods /// /// Reads pixel data from a 1bpp
///
///
Use a
to get a 1bpp bitmap from a more complex image.
private void FromBitmap(Bitmap pageImage) { if (pageImage.PixelFormat != PixelFormat.Format1bppIndexed) { throw new ArgumentException("pageImage.PixelFormat"); } this.Height = pageImage.Height; this.Width = pageImage.Width; this.ImageData = new byte[this.Width, this.Height]; // Extract the pixel data from the bitmap BitmapData data = pageImage.LockBits(new Rectangle(0, 0, pageImage.Width, pageImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format1bppIndexed); int dataLength = data.Stride * data.Height; byte[] bmpData = new byte[dataLength]; Marshal.Copy(data.Scan0, bmpData, 0, dataLength); pageImage.UnlockBits(data); for (int y = 0; y < data.Height; y++) { int bmpDataRowBase = y * data.Stride; for (int x = 0; x < this.Width; x++) { int bmpDataIndex = bmpDataRowBase + (x >> 3); // The index where the pixel is stored sub-byte byte mask = (byte)(0x80 >> (x & 0x07)); int pixel = (bmpData[bmpDataIndex] & mask); this.ImageData[x, y] = (byte)((pixel == 0) ? Constants.PIXEL_VALUE_INK : Constants.PIXEL_VALUE_WHITESPACE); } } } /// /// Create a 32bpp ARGB
from the 1bpp image ///
public virtual Bitmap CreateAsBitmap() { Bitmap retval = new Bitmap(this.Width, this.Height, PixelFormat.Format32bppArgb); BitmapData bmpData = retval.LockBits(new Rectangle(0, 0, this.Width, this.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); int dataLength = bmpData.Stride * bmpData.Height; byte[] data = new byte[dataLength]; for (int y = 0, dataIndex = 0; y < this.Height; y++) { int rowBase = y * bmpData.Stride; for (int x = 0; x < this.Width; x++) { byte pixel = this.ImageData[x, y]; int finalIndex = rowBase + (x * 4); byte r = pixel, g = pixel, b = pixel; // Write the byte values in BGRA order, full opacity for A data[finalIndex] = b; data[finalIndex + 1] = g; data[finalIndex + 2] = r; data[finalIndex + 3] = 0xFF; dataIndex++; } } Marshal.Copy(data, 0, bmpData.Scan0, data.Length); retval.UnlockBits(bmpData); return retval; } #endregion Image read/write methods }}

 

转载于:https://www.cnblogs.com/daxiongblog/p/5578642.html

你可能感兴趣的文章
pytho logging
查看>>
一个Java程序员应该掌握的10项技能
查看>>
c#英文大小写快捷键
查看>>
tpframe免费开源框架又一重大更新
查看>>
一.go语言 struct json相互转换
查看>>
什么是架构设计
查看>>
程序员学习能力提升三要素
查看>>
PHP 微信错误状态返回码说明
查看>>
【4.1】Python中的序列分类
查看>>
ubuntu 移动文件
查看>>
Easy Mock
查看>>
看看 Delphi XE2 为 VCL 提供的 14 种样式
查看>>
Python内置函数(29)——help
查看>>
机器学习系列-tensorflow-01-急切执行API
查看>>
SqlServer 遍历修改字段长度
查看>>
Eclipse快捷键:同时显示两个一模一样的代码窗口
查看>>
《架构之美》阅读笔记05
查看>>
《大道至简》读后感——论沟通的重要性
查看>>
JDBC基础篇(MYSQL)——使用statement执行DQL语句(select)
查看>>
关于React中props与state的一知半解
查看>>