2013-10-13 71 views
1

我在下面編寫代碼我有image2,它是某處的圖片,image3是白色平面中的文本(例如,默認爲白色的paint.exe中寫入的「hello」背景)。通過寫入字節在圖片上寫入文字

我想顯示圖片上的文字,但代碼不成功。問題是什麼?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.IO; 


namespace test_AlignmentOFImages 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
     WriteableBitmap bitmap; 

     private void Window_Loaded(object sender, RoutedEventArgs e) 
     { 
      byte []pixels=new byte[480*640*4]; 

      for (int i = 0; i < pixels.Length; i++) 
      { 
       if (i % 16 == 0) 
       { 
        pixels[i] = 0xff; 
       } 
       else 
       { 
        pixels[i] = (byte)i; 
       } 
       //pixels[i] = (byte)0xff;//white 
      } 
      int stride2 = 480 * 4; 
      image2.Source = BitmapImage.Create((int)image2.Width, (int)image2.Height, 96, 96, PixelFormats.Bgra32, null, pixels, stride2); 

      byte [] imagePixels=new byte[480*640*4]; 
      System.Drawing.Image img; 
      //System.Drawing.Bitmap bm; 
      try 
      { 
       //img = System.Drawing.Image.FromFile(@"E:\src\Tests\test_AlignmentOFImages\test_AlignmentOFImages\image3.jpg"); 
       img = System.Drawing.Image.FromFile(@"E:\src\Tests\test_AlignmentOFImages\test_AlignmentOFImages\image3.png"); 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
      MemoryStream ms=new MemoryStream(); 
      img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); //img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 

      Array.Copy(ms.ToArray(), imagePixels, ms.ToArray().Length); 

      byte[] imagePixels2 = new byte[480 * 640*4]; 
      image3.Source = null; 
      for (int i = 0; i < imagePixels.Length; i+=4) 
      { 
       if (imagePixels[i]<0xff )//if it is not white 
       { 
        imagePixels2[i] = imagePixels[i];//blue 
        imagePixels2[i+1] = imagePixels[i+1];//green 
        imagePixels2[i+2] = imagePixels[i+2];//red 
        imagePixels2[i+3] = 0xff;//alpha 
       } 


      } 

      image3.Source = BitmapImage.Create((int)image3.Width, (int)image3.Height, 96, 96, PixelFormats.Bgra32, null, imagePixels2, stride2); 
     } 
    } 
} 

我認爲我使用虛假的像素格式和PNG或JPEG格式,我必須使用特殊的像素格式(例如bgr24或......)。 在此先感謝。

回答

1
MemoryStream ms=new MemoryStream(); 
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 

用這些行處理像素數據並將其轉換爲PNG文件。但是你想繼續操作像素數據。未格式化的PNG數據。

所以使用LockBits()代替:

imagePixels = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); 

下一個問題是你的複製方法。使用您提供的代碼,只需將image3.png複製到輸出中,丟棄任何Alpha通道並且不考慮白色區域。不要分配一個新的像素數組。使用前面定義的pixels陣列就足夠了。

if (imagePixels[i]<0xff )//if it is not white 

此聲明不檢查像素是否爲白色。它只是檢查一個像素的紅色通道是否是255.你也應該檢查其他通道。