2016-11-08 7 views
0

J'ai cherché une méthode alternative rapide de SetPixel() et je l'ai trouvé ce lien: C# - Faster Alternatives to SetPixel and GetPixel for Bitmaps for Windows Forms AppConvertir ARVB à PARGB

Donc mon problème est que j'ai une image et je veux créer une copie comme un objet DirectBitmap mais d'abord je dois convertir ARVB à PARGB donc j'utilisé ce code:

public static Color PremultiplyAlpha(Color pixel) 
    { 
     return Color.FromArgb(
      pixel.A, 
      PremultiplyAlpha_Component(pixel.R, pixel.A), 
      PremultiplyAlpha_Component(pixel.G, pixel.A), 
      PremultiplyAlpha_Component(pixel.B, pixel.A)); 
    } 

    private static byte PremultiplyAlpha_Component(byte source, byte alpha) 
    { 
     return (byte)((float)source * (float)alpha/(float)byte.MaxValue + 0.5f); 
    } 

et voici mon code copie:

DirectBitmap DBMP = new DirectBitmap(img.Width, img.Height); 
     MyImage myImg = new MyImage(img as Bitmap); 

     for (int i = 0; i < img.Width; i++) 
     { 
      for (int j = 0; j < img.Height; j++) 
      { 
       Color PARGB = NativeWin32.PremultiplyAlpha(Color.FromArgb(myImg.RGB[i, j].Alpha, 
        myImg.RGB[i, j].R, myImg.RGB[i, j].G, myImg.RGB[i, j].B)); 

       byte[] bitMapData = new byte[4]; 
       bitMapData[3] = (byte)PARGB.A; 
       bitMapData[2] = (byte)PARGB.R; 
       bitMapData[1] = (byte)PARGB.G; 
       bitMapData[0] = (byte)PARGB.B; 


       DBMP.Bits[(i * img.Height) + j] = BitConverter.ToInt32(bitMapData, 0); 
      } 
     } 

MonImage: une classe contenant un o Bitmap bject avec un tableau de struct RGB stockant les couleurs de chaque pixel

Cependant, ce code me donne une image foirée. Qu'est-ce que je fais mal?

+0

Qu'est-ce que PARGB? (sans jeu de mots. Juste curieux) –

+0

Alpha prémultiplié, s'il vous plaît lire la réponse dans le premier lien – Eslam

Répondre

0

Les données bitmap sont organisées en ligne horizontale après ligne horizontale. Par conséquent, votre dernière ligne doit être:

DBMP.Bits[j * img.Width + i] = BitConverter.ToInt32(bitMapData, 0); 
+0

Merci! fonctionne très bien :) – Eslam