BitmapSource 是WPF中图片的核心类型,读、写、显示都很常用,下面所有的操作都是以这个类型为核心的。
从文件读如图片到BitmapSource 类型:
1 private BitmapSource GetSource()
2 {
3 BitmapSource result = null;
4
5 OpenFileDialog dialog = new OpenFileDialog();
6 dialog.Filter = "常用位图(*.bmp;*.jpg;*.png)|*.bmp;*.jpg;*.png";
7
8 if (true == dialog.ShowDialog() && File.Exists(dialog.FileName))
9 {
10 FileStream imageStreamSource = File.OpenRead(dialog.FileName);
11 BitmapDecoder decoder = BitmapDecoder.Create(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
12 result = decoder.Frames[0];
13 }
14
15 return result;
16 }
写文件:
1 private void OutPut_Click(object sender, RoutedEventArgs e)
2 {
3
4 SaveFileDialog dialog = new SaveFileDialog();
5 dialog.Filter = "图片(*.jpg)|*.jpg";
6 dialog.FileName = Title;
7 dialog.Title = "保存图片";
8
9 if (dialog.ShowDialog() == true)
10 {
11 // 利用JpegBitmapEncoder,对图像进行编码,以便进行保存
12 JpegBitmapEncoder encoder = new JpegBitmapEncoder();
13 encoder.Frames.Add(BitmapFrame.Create(_image));
14 // 保存文件
15 FileStream fileStream = new FileStream(dialog.FileName, FileMode.Create, FileAccess.ReadWrite);
16 encoder.Save(fileStream);
17 // 关闭文件流
18 fileStream.Close();
19
20 MessageBox.Show("保存成功!");
21 }
22 }
显示:
1. Image 控件的 Source 属性设置即可
2. 其他控件的背景设置:this.Background = new ImageBrush(image);//image 是 BitmapSource 类型
处理:
这里奉上一个类,还有些问题,欢迎拍砖
/// <summary>
/// 像素图像
/// </summary>
public class ByteImage
{
#region 字段
byte[] _pixls;
int _height;
int _width;
int _stride;
int _bytePerPixel;
double _dpix;
double _dpiy;
PixelFormat _format;
BitmapPalette _palette;
#endregion
#region 访问器
public PixelFormat Format
{
get { return _format; }
set { _format = value; }
}
public double Dpiy
{
get { return _dpiy; }
set { _dpiy = value; }
}
public double Dpix
{
get { return _dpix; }
set { _dpix = value; }
}
public BitmapPalette Palette
{
get { return _palette; }
set { _palette = value; }
}
public int Height
{
get { return _height; }
}
public int Width
{
get { return _width; }
}
public int Stride
{
get { return _stride; }
}
public byte[] Pixels
{
get { return _pixls; }
set { _pixls = value; }
}
public int BytePerPixel
{
get { return _bytePerPixel; }
}
#endregion
#region 函数
/// <summary>
/// 像素图像
/// </summary>
public ByteImage(BitmapSource image)
{
_width = image.PixelWidth;
_height = image.PixelHeight;
_bytePerPixel = image.Format.BitsPerPixel / 8;
_stride = _width * _bytePerPixel;//跨距 图片的一行
_pixls = new byte[_stride * _height];
_dpix = image.DpiX;
_dpiy = image.DpiY;
_format = image.Format;
_palette = null;
image.CopyPixels(_pixls, _stride, 0);
}
/// <summary>
/// 获得一个像素的颜色信息 超界返回 nul
/// </summary>
/// <param name="x">X</param>
/// <param name="y">Y</param>
/// <returns>颜色信息 byte[]</returns>
public byte[] GetPixel(int x, int y)
{
byte[] pixel = null;
if (x < Height && y < Width && x >= 0 && y >= 0)
{
int startPostion = Stride * x + y * BytePerPixel;
pixel = new byte[BytePerPixel];
for (int i = 0; i < BytePerPixel; i++)
{
pixel[i] = Pixels[startPostion + i];
}
}
return pixel;
}
/// <summary>
/// 设置一个像素的颜色信息
/// </summary>
/// <param name="pixel">颜色信息</param>
/// <param name="x">X</param>
/// <param name="y">Y</param>
public void SetPixel(byte[] pixel, int x, int y)
{
int startPostion = Stride * x + y * BytePerPixel;
if (x < Height && y < Width && x >= 0 && y >= 0 && pixel.Length == BytePerPixel)
{
for (int i = 0; i < BytePerPixel; i++)
{
Pixels[startPostion + i] = pixel[i];
}
}
}
/// <summary>
/// 转换成位图
/// </summary>
/// <param name="dpix">水平DPI</param>
/// <param name="dpiy">垂直DPI</param>
/// <param name="format">格式</param>
/// <param name="palette">调色板</param>
/// <returns></returns>
public BitmapSource ToBitmapSource(double dpix, double dpiy, PixelFormat format, BitmapPalette palette = null)
{
return BitmapSource.Create(Width, Height, dpix, dpiy, format, palette, Pixels, Stride);
}
/// <summary>
/// 转换成位图
/// </summary>
public BitmapSource ToBitmapSource()
{
return BitmapSource.Create(Width, Height, _dpix,_dpiy, _format, _palette, Pixels, Stride);
}
#endregion
}
这个类通过一个 BitmapSource构造 并可以输出成 BitmapSource 类型。它的主要作用是架起在 BitmapSource与byte[] 之间的桥梁,在两者之间进行转换,并提供按照矩阵方式设置或读取一个像素点的方法,为后续基于像素的处理提供方便。
灰化:
1 private BitmapSource ToGray(BitmapSource source)
2 {
3 FormatConvertedBitmap re = new FormatConvertedBitmap();
4 re.BeginInit();
5 re.Source = source;
6 re.DestinationFormat = PixelFormats.Gray8;
7 re.EndInit();
8 return re;
9 }
祝各位,学习进步,工作愉快,身体健康!