WPF下的簡單數字圖像處理


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 }


祝各位,學習進步,工作愉快,身體健康!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM