Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - C# Le Bitmap fanno i capricci
Forum - C# / VB.NET - C# Le Bitmap fanno i capricci

Avatar
Fharamir (Normal User)
Rookie


Messaggi: 21
Iscritto: 06/05/2011

Segnala al moderatore
Postato alle 14:30
Giovedì, 23/05/2013
Buondì a tutti,

è molto che non scrivo sul forum, di solito trovo in rete le soluzioni ai miei problemi...
Questa volta è diverso, il mio problema così insolito che sono convinto di aver scritto qualche cafonata tremenda che non riesco a trovare (ovviamente da una parte spero non lo sia per evitare figuracce).

Vi spiego il problema:
Il mio programma fa, o almeno dovrebbe fare, alcune semplici operazioni. Una volta caricata un'immagine da file (bmp, jpg o png) il programma ne crea altre 9.
La prima è uguale all'originale ma ridimensionata.
Le 8 successive sono copie dell'originale e della ridimensionata, ma ricolorate (bianco e nero o gialla ecc...).

Queste semplici operazioni però non sono, evidentemente, così semplici.

Quello che ottengo è che ogni volta che creo una delle 8 immagini l'original e la ridimensionata vengono ricolorate a loro volta, ottenendo quindi 8 volte lo stesso risultato.

Con un esempio forse il problema si comprende meglio:

Ciò che vorrei ottenere:
http://imageshack.us/a/img189/3079/immaginejvu.png

Ciò che invece ottengo:
http://imageshack.us/a/img211/2972/bady.png

Il software è strutturato così:

Primo form dove selezionare l'immagine da caricare
Secondo form dove vengono visualizzate le 4 opzioni colorate diversamente
Classe "ImgHolder" che si occupa della creazione di tutte le immagini necessarie.

Ometterò nel codice ciò che non riguarda questo problema, se può servire il sorgente completo lo metterò senza problemi.

Form1:
Codice sorgente - presumibilmente C++

  1. [CODE]
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10.  
  11. namespace Fast_Recolorer
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.  
  16.         ImgHolder IH;
  17.         private Options fr2;
  18.  
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         private void BaseImg_Click(object sender, EventArgs e)
  25.         {
  26.             OpenFileDialog ImgLoad = new OpenFileDialog();
  27.             ImgLoad.Filter = "Bitmap File|*.bmp|JPG File|*.jpg|PNG File|*.png";
  28.             ImgLoad.Title = "Select an Image File";
  29.  
  30.             if (ImgLoad.ShowDialog() == DialogResult.OK)
  31.             {
  32.                 IH = new ImgHolder(ImgLoad.FileName);
  33.  
  34.                 Recalibrate();
  35.  
  36.                 BaseImg.Image = IH.Resized;
  37.             }
  38.         }
  39.  
  40.         private void Recalibrate()
  41.         {
  42.             // not needed!
  43.         }
  44.  
  45.         private void Do_Click(object sender, EventArgs e)
  46.         {
  47.             fr2 = new Options(IH);
  48.             fr2.Show();
  49.         }
  50.     }
  51. }
  52. [/CODE]



Form Options:
Codice sorgente - presumibilmente C++

  1. [CODE]
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10.  
  11. namespace Fast_Recolorer
  12. {
  13.     public partial class Options : Form
  14.     {
  15.         public ImgHolder IH;
  16.        
  17.  
  18.         public Options(ImgHolder I)
  19.         {
  20.             InitializeComponent();
  21.             IH = I;
  22.         }
  23.  
  24.         private void Options_Load(object sender, EventArgs e)
  25.         {
  26.             Opt1.Height = IH.Resized.Height;
  27.             Opt2.Height = IH.Resized.Height;
  28.             Opt3.Height = IH.Resized.Height;
  29.  
  30.             this.Height = this.Height - (Opt4.Height - Opt3.Height);
  31.  
  32.             Opt4.Height = IH.Resized.Height;
  33.  
  34.             Opt1.Image = IH.BW_R;
  35.             Opt2.Image = IH.PP_R;
  36.             Opt3.Image = IH.YW_R;
  37.             Opt4.Image = IH.CY_R;
  38.         }
  39.     }
  40. }
  41. [/CODE]



Classe ImgHolder:
Codice sorgente - presumibilmente C++

  1. [CODE]
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Drawing;
  7.  
  8. namespace Fast_Recolorer
  9. {
  10.     public class ImgHolder
  11.     {
  12.         public Bitmap Source;
  13.         public Bitmap Resized;
  14.         public Bitmap BW_R, PP_R, YW_R, CY_R;
  15.         public Bitmap BW, PP, YW, CY;
  16.  
  17.         public ImgHolder(string path)
  18.         {
  19.             Source = new Bitmap(Bitmap.FromFile(path));
  20.             Set_Resized();
  21.  
  22.             BW_R = Black_White(Resized);
  23.             PP_R = To_Purple(Resized);
  24.             YW_R = To_Yellow(Resized);
  25.             CY_R = To_Cyan(Resized);
  26.  
  27.             BW = Black_White(Source);
  28.             PP = To_Purple(Source);
  29.             YW = To_Yellow(Source);
  30.             CY = To_Cyan(Source);
  31.         }
  32.  
  33.         private void Set_Resized()
  34.         {
  35.             double ratio = (double)Source.Width / 256;
  36.             int y = Convert.ToInt32((double)Source.Height / ratio);
  37.  
  38.             Resized = new Bitmap(Source, new Size(256, y));
  39.         }
  40.  
  41.         public Bitmap Black_White(Bitmap A)
  42.         {
  43.             for (int y = 0; y < A.Height - 1; y++)
  44.                 for (int x = 0; x < A.Width - 1; x++)
  45.                 {
  46.                     short r = A.GetPixel(x, y).R;
  47.                     short g = A.GetPixel(x, y).G;
  48.                     short b = A.GetPixel(x, y).B;
  49.  
  50.                     r = Convert.ToInt16((r + g + b) / 3);
  51.  
  52.                     A.SetPixel(x, y, Color.FromArgb(r, r, r));
  53.                 }
  54.             return A;
  55.         }
  56.  
  57.         public Bitmap To_Purple(Bitmap A)
  58.         {
  59.             for (int y = 0; y < A.Height - 1; y++)
  60.                 for (int x = 0; x < A.Width - 1; x++)
  61.                 {
  62.                     short r = A.GetPixel(x, y).R;
  63.                     short g = A.GetPixel(x, y).G;
  64.                     short b = A.GetPixel(x, y).B;
  65.  
  66.                     r = Convert.ToInt16((r + g + b) / 3);
  67.  
  68.                     A.SetPixel(x, y, Color.FromArgb(r, 0, r));
  69.                 }
  70.             return A;
  71.         }
  72.  
  73.         public Bitmap To_Yellow(Bitmap A)
  74.         {
  75.             for (int y = 0; y < A.Height - 1; y++)
  76.                 for (int x = 0; x < A.Width - 1; x++)
  77.                 {
  78.                     short r = A.GetPixel(x, y).R;
  79.                     short g = A.GetPixel(x, y).G;
  80.                     short b = A.GetPixel(x, y).B;
  81.  
  82.                     r = Convert.ToInt16((r + g + b) / 3);
  83.  
  84.                     A.SetPixel(x, y, Color.FromArgb(r, r, 0));
  85.                 }
  86.             return A;
  87.         }
  88.  
  89.         public Bitmap To_Cyan(Bitmap A)
  90.         {
  91.             for (int y = 0; y < A.Height - 1; y++)
  92.                 for (int x = 0; x < A.Width - 1; x++)
  93.                 {
  94.                     short r = A.GetPixel(x, y).R;
  95.                     short g = A.GetPixel(x, y).G;
  96.                     short b = A.GetPixel(x, y).B;
  97.  
  98.                     r = Convert.ToInt16((r + g + b) / 3);
  99.  
  100.                     A.SetPixel(x, y, Color.FromArgb(0, r, r));
  101.                 }
  102.             return A;
  103.         }
  104.     }
  105. }
  106. [/CODE]


Ultima modifica effettuata da Fharamir il 23/05/2013 alle 14:36
PM Quote
Avatar
Fharamir (Normal User)
Rookie


Messaggi: 21
Iscritto: 06/05/2011

Segnala al moderatore
Postato alle 14:49
Giovedì, 23/05/2013
Azz!!

Ho risolto!

Chiedo perdono per il disturbo!! :_doubt::_doubt:

Il problema era nell'inizializzazione delle Bitmap e nei metodi che modificavano l'originale
(nonostante il passaggio dei valori non fosse per riferimento.. bah)

Se può servire a sbeffeggiarmi allego il sorgente corretto:
Codice sorgente - presumibilmente C++

  1. [CODE]
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Drawing;
  7.  
  8. namespace Fast_Recolorer
  9. {
  10.     public class ImgHolder
  11.     {
  12.         public Bitmap Source;
  13.         public Bitmap Resized;
  14.         public Bitmap BW_R, PP_R, YW_R, CY_R;
  15.         public Bitmap BW, PP, YW, CY;
  16.  
  17.         public ImgHolder(string path)
  18.         {
  19.             Source = new Bitmap(Bitmap.FromFile(path));
  20.             Set_Resized();
  21.  
  22.             BW_R = new Bitmap(Black_White(Resized));
  23.             PP_R = new Bitmap(To_Purple(Resized));
  24.             YW_R = new Bitmap(To_Yellow(Resized));
  25.             CY_R = new Bitmap(To_Cyan(Resized));
  26.  
  27.             BW = new Bitmap(Black_White(Source));
  28.             PP = new Bitmap(To_Purple(Source));
  29.             YW = new Bitmap(To_Yellow(Source));
  30.             CY = new Bitmap(To_Cyan(Source));
  31.         }
  32.  
  33.         private void Set_Resized()
  34.         {
  35.             double ratio = (double)Source.Width / 256;
  36.             int y = Convert.ToInt32((double)Source.Height / ratio);
  37.  
  38.             Resized = new Bitmap(Source, new Size(256, y));
  39.         }
  40.  
  41.         public Bitmap Black_White(Bitmap A)
  42.         {
  43.             Bitmap B = new Bitmap(A);
  44.  
  45.             for (int y = 0; y < A.Height - 1; y++)
  46.                 for (int x = 0; x < A.Width - 1; x++)
  47.                 {
  48.                     short r = A.GetPixel(x, y).R;
  49.                     short g = A.GetPixel(x, y).G;
  50.                     short b = A.GetPixel(x, y).B;
  51.  
  52.                     r = Convert.ToInt16((r + g + b) / 3);
  53.  
  54.                     B.SetPixel(x, y, Color.FromArgb(r, r, r));
  55.                 }
  56.             return B;
  57.         }
  58.  
  59.         public Bitmap To_Purple(Bitmap A)
  60.         {
  61.             Bitmap B = new Bitmap(A);
  62.  
  63.             for (int y = 0; y < A.Height - 1; y++)
  64.                 for (int x = 0; x < A.Width - 1; x++)
  65.                 {
  66.                     short r = A.GetPixel(x, y).R;
  67.                     short g = A.GetPixel(x, y).G;
  68.                     short b = A.GetPixel(x, y).B;
  69.  
  70.                     r = Convert.ToInt16((r + g + b) / 3);
  71.  
  72.                     B.SetPixel(x, y, Color.FromArgb(r, 0, r));
  73.                 }
  74.             return B;
  75.         }
  76.  
  77.         public Bitmap To_Yellow(Bitmap A)
  78.         {
  79.             Bitmap B = new Bitmap(A);
  80.  
  81.             for (int y = 0; y < A.Height - 1; y++)
  82.                 for (int x = 0; x < A.Width - 1; x++)
  83.                 {
  84.                     short r = A.GetPixel(x, y).R;
  85.                     short g = A.GetPixel(x, y).G;
  86.                     short b = A.GetPixel(x, y).B;
  87.  
  88.                     r = Convert.ToInt16((r + g + b) / 3);
  89.  
  90.                     B.SetPixel(x, y, Color.FromArgb(r, r, 0));
  91.                 }
  92.             return B;
  93.         }
  94.  
  95.         public Bitmap To_Cyan(Bitmap A)
  96.         {
  97.             Bitmap B = new Bitmap(A);
  98.  
  99.             for (int y = 0; y < A.Height - 1; y++)
  100.                 for (int x = 0; x < A.Width - 1; x++)
  101.                 {
  102.                     short r = A.GetPixel(x, y).R;
  103.                     short g = A.GetPixel(x, y).G;
  104.                     short b = A.GetPixel(x, y).B;
  105.  
  106.                     r = Convert.ToInt16((r + g + b) / 3);
  107.  
  108.                     B.SetPixel(x, y, Color.FromArgb(0, r, r));
  109.                 }
  110.             return B;
  111.         }
  112.     }
  113. }
  114. [/CODE]


Ultima modifica effettuata da Fharamir il 23/05/2013 alle 14:50
PM Quote