J'ai un tableau d'octets de pixels dans l'ordre BGR et je veux créer une image en C#. Quelqu'un peut-il offrir du code ou des conseils?Programmation de pixels suite
5
A
Répondre
1
Quelque chose à l'effet de (non testé)
private Bitmap createImage(int width, int height, byte[] image)
{
int index = 0;
byte r, g, b;
Bitmap bmp = new Bitmap(width, height);
for (y as int = 0; y < height; y++)
{
for (x as int = 0; x < width; x++)
{
b = image[y * width + index];
g = image[y * width + index + 1];
r = image[y * width + index + 2];
bmp.SetPixel(x, y, Color.FromArgb(255, r, g, b));
index += 3;
}
}
return bmp;
}
0
Le constructeur Bitmap(int width, int height, int stride, PixelFormat format, IntPtr scan0)
pour la classe Bitmap
pourrait être utile. Cela dépend de la façon dont les données sont stockées dans le tableau, bien sûr.
1
Vous pourriez avoir besoin quelque chose comme ceci:
public static Bitmap TransformBGRArrayToBitmap(byte[] inputValues, int Width, int Height, int Stride)
{
Bitmap output = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, Width, Height);
BitmapData outputData = output.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, output.PixelFormat);
// Get the address of the first line.
IntPtr outputPtr = outputData.Scan0;
// Declare an array to hold the bytes of the bitmap.
byte[] outputValues = new byte[outputData.Stride * output.Height];
int inputBytesPP = (1 * Stride)/Width;
int outputBytesBPP = (1 * outputData.Stride)/output.Width;
// Copy the RGB values into the array.
for (int inputByte = 0, outputByte = 0; inputByte < inputValues.Length; inputByte += inputBytesPP, outputByte += outputBytesBPP)
{
//The logic inside this loop transforms a 32 bit ARGB Bitmap into an 8 bit indexed Bitmap
//So you will have to replace it
/*byte pixel = 0x00;
if (inputValues[inputByte] > 0x7F)
{
if (inputValues[inputByte + 1] > 0x7F)
pixel |= 0x01;
if (inputValues[inputByte + 2] > 0x7F)
pixel |= 0x02;
if (inputValues[inputByte + 3] > 0x7F)
pixel |= 0x04;
if ((inputValues[inputByte + 1] & 0x7F) > 0x3F)
pixel |= 0x02;
if ((inputValues[inputByte + 2] & 0x7F) > 0x3F)
pixel |= 0x04;
if ((inputValues[inputByte + 3] & 0x7F) > 0x3F)
pixel |= 0x08;
}
else
pixel = 0x10;
outputValues[outputByte] = pixel;*/
}
System.Runtime.InteropServices.Marshal.Copy(outputValues, 0, outputPtr, outputValues.Length);
output.UnlockBits(outputData);
return output;
}
Questions connexes
- 1. Suite de lignes d'analyse
- 2. Suite sélectionnant trop de colonnes
- 3. Algorithmes de traitement des pixels
- 4. Quelle technique en programmation fonctionnelle est difficile à apprendre mais utile par la suite?
- 5. Suite à l'exception
- 6. ASP.Net Media Suite - Revue
- 7. Python 2.5.2 suite
- 8. AS3 ... (suite) Paramètre
- 9. Comment déplacer les pixels?
- 10. Suite de tests de conformité HTTP
- 11. C# pratique de programmation
- 12. Suite de tests Fitnesse avec variables
- 13. didacticiel de la suite d'outils springsource (éclipse)?
- 14. projets de déploiement Web, Team Suite 2008
- 15. Suite de test pour double metaphone?
- 16. C: bibliothèque de threads POSIX test-suite
- 17. Ordre SQL par problème-Suite
- 18. Recherche de pixels par pouce en wxDC
- 19. Taille des pixels od NSBitmapImageRep
- 20. CodeSmith v.s. T4: .netTiers level suite
- 21. Questions sur OO en PHP suite
- 22. Suite à des enregistrements avec ActiveRecord?
- 23. Optimiser le calcul d'une suite récurrente
- 24. Rails - Deux voies modèle "amitié" (suite)
- 25. Comment créer une suite Junit4 avec Groovy?
- 26. Associate .Rnw avec vim latex suite
- 27. Recommandations - Programmation de logiciels/composants .Net
- 28. Actionscript3: comment lister les pixels d'une forme?
- 29. Conversion de pixels en courbes de Bézier dans Actionscript 3
- 30. Obtention de données de pixels .png dans Silverlight
Pourriez-vous s'il vous plaît donner un échantillon du tableau afin que nous sachions ses dimensions? Je pense que la manipulation de matrice sera nécessaire pour modifier le contenu du tableau avant de créer le bitmap. –