2013-10-13 2 views
1

아래에 코드가 있습니다. 나는 어딘가에있는 그림 인 image2와 흰색 평면에있는 텍스트 인 image3을 가지고 있습니다. 예를 들어, "hello"는 기본 흰색 인 paint.exe로 작성되었습니다. 배경).그림에 문자를 쓰면서 문자를 쓰면서 그림을 그릴 수 있습니다.

그림에 텍스트를 표시하고 싶지만 코드가 성공하지 못합니다. 문제가 무엇입니까?

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를 출력에 복사하고 알파 채널은 삭제하고 흰색 영역은 고려하지 않습니다. 새로운 픽셀 배열을 할당하지 마십시오. 앞에서 정의한 pixels 어레이를 사용하면 충분합니다.

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

이 문장은 픽셀이 흰색인지 확인하지 않습니다. 픽셀의 빨간 채널이 255인지 확인합니다. 다른 채널도 확인해야합니다.

관련 문제