2015-05-21 12 views
5

प्रोग्राम यह https://www.youtube.com/watch?v=l1_WmoiKgPg वीडियो का केवल पहला 50 सेकंड दूसरा टैब समाप्त हो गया है :) तो मूल रूप से आप सबसे लाल रंग के साथ रंग का चयन करते हैं, तो यदि यह सबसे हरा होता है तो सबसे अधिक नीला। लेकिन नए पिक्सल नई छवि पर क्यों नहीं लिखे गए हैं क्यों? और यादृच्छिक वर्ग के साथ पहला रेडियो बटन कैसे करें?पिक्सल को एक नए लिखने योग्य बिटमैप में लिखना

मेरे कस्टम विधि:

private void writepixel(WriteableBitmap ElWriteableBitmap, int iCoordX, int iCoordY, byte bRojo, byte bVerde, byte bAzul, byte bAlfa) 
{ 
    unsafe 
    { 
     int ipTempBackBuffer = (int)ElWriteableBitmap.BackBuffer; 
     ipTempBackBuffer += iCoordY * ElWriteableBitmap.BackBufferStride; 
     ipTempBackBuffer += iCoordX * 4; 

     int color_data = bAlfa << 24; // Canal alfa 
     color_data |= bRojo << 16; // Componente rojo 
     color_data |= bVerde << 8; // Componente verde 
     //color_data = color_data & (bVerde << 8); 
     color_data |= bAzul << 0; // Componente azul 

     *((int*)ipTempBackBuffer) = color_data; 
    } 
} 

private System.Windows.Media.Color readPixel(WriteableBitmap ElWriteableBitmap, int iCoordX, int iCoordY) 
{ 
    System.Windows.Media.Color colorResultante = new System.Windows.Media.Color(); 
    unsafe 
    { 
     int ipTempBackBuffer = (int)ElWriteableBitmap.BackBuffer; 
     ipTempBackBuffer += iCoordY * ElWriteableBitmap.BackBufferStride; 
     ipTempBackBuffer += iCoordX * 4; 

     colorResultante.A = ((byte*)ipTempBackBuffer)[3]; 
     colorResultante.R = ((byte*)ipTempBackBuffer)[2]; 
     colorResultante.G = ((byte*)ipTempBackBuffer)[1]; 
     colorResultante.B = ((byte*)ipTempBackBuffer)[0]; 
    } 
    return colorResultante; 
} 

2 छवियों

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    OpenFileDialog ofd = new OpenFileDialog(); 
    ofd.FileName = "Imagen"; 
    ofd.DefaultExt = ".bmp"; 
    ofd.Filter = "Mapas de bits|*.bmp"; 
    Nullable<bool> result = ofd.ShowDialog(); 
    if (result == true) 
    { 
     BitmapImage bmpi = new BitmapImage(); //JpegBitmapEncoder alternativa 
     bmpi.BeginInit(); 
     bmpi.UriSource = new Uri(ofd.FileName); 
     bmpi.EndInit(); 
     m_wbmpLaImagen = new WriteableBitmap(bmpi); 
     laimagen1.Source = m_wbmpLaImagen; 
    } 
} 

private void Button_Click_1(object sender, RoutedEventArgs e) 
{ 
    OpenFileDialog ofd = new OpenFileDialog(); 
    ofd.FileName = "Imagen"; 
    ofd.DefaultExt = ".bmp"; 
    ofd.Filter = "Mapas de bits|*.bmp"; 
    Nullable<bool> result = ofd.ShowDialog(); 
    if (result == true) 
    { 
     BitmapImage bmpi = new BitmapImage(); //JpegBitmapEncoder alternativa 
     bmpi.BeginInit(); 
     bmpi.UriSource = new Uri(ofd.FileName); 
     bmpi.EndInit(); 
     m_wbmpLaImagen = new WriteableBitmap(bmpi); 
     laimagen2.Source = m_wbmpLaImagen; 
    } 
} 

मेरे यादृच्छिक वर्ग और दो रेडियो बटन के लिए कोड अपलोड किया गया:

private static int[] randomNumber() 
{ 
    Random rand = new Random(); 
    int[] nums = new int[20]; 
    for (int x = 0; x < 1; x++) 
    { 
     nums[x] = rand.Next(); 
    } 
    return nums; 
} 

private void RadioButton_Checked(object sender, RoutedEventArgs e) 
{ 
    WriteableBitmap imagen1bmp; 
    WriteableBitmap imagen2bmp; 
    WriteableBitmap imgcom; 
    imgcom = (WriteableBitmap)imgcombined.Source; 
    int x, y; 
    Color col3 = new Color(); 
    imagen1bmp = (WriteableBitmap)laimagen1.Source; 
    imagen2bmp = (WriteableBitmap)laimagen2.Source;   

    for (y = 0; y < imagen1bmp.Height; y++) 
    { 
     for (x = 0; x < imagen1bmp.Width; x++) 
     { 
      // Get both colors in the pixel point 
      Color col1 = readPixel(imagen1bmp, x, y); 
      Color col2 = readPixel(imagen2bmp, x, y); 
      // the color for the output image for this pixel 
      if (col1.R > col2.R) 
       col3.R = col1.R; 
      else 
       col3.R = col2.R; 
      if (col1.G > col2.G) 
       col3.G = col1.G; 
      else 
       col3.G = col2.G; 

      if (col1.B > col2.B) 
       col3.B = col1.B; 
      else 
       col3.B = col2.B; 

      if (col1.A > col2.A) 
       col3.A = col1.A; 
      else 
       col3.A = col2.A; 

      writepixel(imgcom, x, y, col3.R, col3.G, col3.B, col3.A); 
     } 
     UpdateLayout(); 
    } 
    UpdateLayout(); 
} 

उत्तर

0

में चार त्रुटियां नहीं हैं अपने कोड:

त्रुटि 1) imgcom सही आयामों के एक नए बिटमैप के साथ तत्काल नहीं किया जा रहा था। समाधान:

imgcom = new WriteableBitmap(imagen1bmp); 
// Make changes... 
imgcombined.Source = imgcom; 

त्रुटि 2) छोरों प्रदर्शन पिक्सेल आयाम (Width और Height) के बजाय आंतरिक बफर आयाम (PixelWidth और PixelHeight) का उपयोग कर रहे थे। समाधान:

for (y = 0; y < imagen1bmp.PixelHeight; y++) 
for (x = 0; x < imagen1bmp.PixelWidth; x++) 

त्रुटि 3) गंतव्य बफर बंद होने नहीं किया गया था और लेखन के आसपास अनलॉक किया गया। समाधान:

imgcom.Lock(); 
// Change the pixels... 
imgcom.Unlock(); 

त्रुटि 4) बफर में ऑफसेट बफर के एक int सूचक के साथ BackBufferStride का उपयोग कर किया गया। यह गलत है क्योंकि बफर स्ट्रैड बाइट्स की संख्या को संदर्भित करता है, पिक्सेल नहीं। समाधान:

ipTempBackBuffer += (iCoordY * ElWriteableBitmap.PixelWidth) + iCoordX; 

कार्यात्मक परिणाम:

private WriteableBitmap m_wbmpLaImagen; 

public MainWindow() 
{ 
    InitializeComponent(); 
} 

private void button_load_1_Click(object sender, RoutedEventArgs e) 
{ 
    OpenFileDialog ofd = new OpenFileDialog(); 
    ofd.FileName = "Imagen"; 
    ofd.DefaultExt = ".bmp"; 
    ofd.Filter = "Mapas de bits|*.bmp"; 
    Nullable<bool> result = ofd.ShowDialog(); 
    if (result == true) 
    { 
     BitmapImage bmpi = new BitmapImage(); //JpegBitmapEncoder alternativa 
     bmpi.BeginInit(); 
     bmpi.UriSource = new Uri(ofd.FileName); 
     bmpi.EndInit(); 
     m_wbmpLaImagen = new WriteableBitmap(bmpi); 
     laimagen1.Source = m_wbmpLaImagen; 
    } 
} 

private void button_load_2_Click(object sender, RoutedEventArgs e) 
{ 
    OpenFileDialog ofd = new OpenFileDialog(); 
    ofd.FileName = "Imagen"; 
    ofd.DefaultExt = ".bmp"; 
    ofd.Filter = "Mapas de bits|*.bmp"; 
    Nullable<bool> result = ofd.ShowDialog(); 
    if (result == true) 
    { 
     BitmapImage bmpi = new BitmapImage(); //JpegBitmapEncoder alternativa 
     bmpi.BeginInit(); 
     bmpi.UriSource = new Uri(ofd.FileName); 
     bmpi.EndInit(); 
     m_wbmpLaImagen = new WriteableBitmap(bmpi); 
     laimagen2.Source = m_wbmpLaImagen; 
    } 
} 

private void button_do_processing_Click(object sender, RoutedEventArgs e) 
{ 
    WriteableBitmap imagen1bmp; 
    WriteableBitmap imagen2bmp; 
    WriteableBitmap imgcom; 
    int x, y; 
    Color col3 = new Color(); 
    imagen1bmp = (WriteableBitmap)laimagen1.Source; 
    imagen2bmp = (WriteableBitmap)laimagen2.Source; 
    imgcom = new WriteableBitmap(imagen1bmp); 

    imgcom.Lock();// Prepare BackBuffer for writing; 
    for (y = 0; y < imagen1bmp.PixelHeight; y++) 
    { 
     for (x = 0; x < imagen1bmp.PixelWidth; x++) 
     { 
      // Get both colors in the pixel point 
      Color col1 = readPixel(imagen1bmp, x, y); 
      Color col2 = readPixel(imagen2bmp, x, y); 
      // the color for the output image for this pixel 
      if (col1.R > col2.R) 
       col3.R = col1.R; 
      else 
       col3.R = col2.R; 
      if (col1.G > col2.G) 
       col3.G = col1.G; 
      else 
       col3.G = col2.G; 

      if (col1.B > col2.B) 
       col3.B = col1.B; 
      else 
       col3.B = col2.B; 

      if (col1.A > col2.A) 
       col3.A = col1.A; 
      else 
       col3.A = col2.A; 

      writepixel(imgcom, x, y, col3.R, col3.G, col3.B, col3.A); 
     } 
     UpdateLayout(); 
    } 
    imgcom.Unlock();// Release BackBuffer from writing; 
    UpdateLayout(); 
    imgcombined.Source = imgcom; 
} 

private static int[] randomNumber() 
{ 
    Random rand = new Random(); 
    int[] nums = new int[20]; 
    for (int x = 0; x < 1; x++) 
    { 
     nums[x] = rand.Next(); 
    } 
    return nums; 
} 

private void writepixel(WriteableBitmap ElWriteableBitmap, int iCoordX, int iCoordY, byte bRojo, byte bVerde, byte bAzul, byte bAlfa) 
{ 
    unsafe 
    { 
     int *ipTempBackBuffer = (int *)ElWriteableBitmap.BackBuffer; 
     ipTempBackBuffer += (iCoordY * ElWriteableBitmap.PixelWidth) + iCoordX; 

     int color_data = bAlfa << 24; // Canal alfa 
     color_data |= bRojo << 16; // Componente rojo 
     color_data |= bVerde << 8; // Componente verde 
     //color_data = color_data & (bVerde << 8); 
     color_data |= bAzul << 0; // Componente azul 

     *ipTempBackBuffer = color_data; 
    } 
} 

private System.Windows.Media.Color readPixel(WriteableBitmap ElWriteableBitmap, int iCoordX, int iCoordY) 
{ 
    System.Windows.Media.Color colorResultante = new System.Windows.Media.Color(); 
    unsafe 
    { 
     int *ipTempBackBuffer = (int *)ElWriteableBitmap.BackBuffer; 
     ipTempBackBuffer += (iCoordY * ElWriteableBitmap.PixelWidth) + iCoordX; 

     colorResultante.A = ((byte *)ipTempBackBuffer)[3]; 
     colorResultante.R = ((byte *)ipTempBackBuffer)[2]; 
     colorResultante.G = ((byte *)ipTempBackBuffer)[1]; 
     colorResultante.B = ((byte *)ipTempBackBuffer)[0]; 
    } 
    return colorResultante; 
} 
संबंधित मुद्दे