Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- using Xamarin.Forms;
- using System.Text;
- using System.Collections.Generic;
- namespace MandelBrootSet
- {
- public class BmpMaker
- {
- const int headerSize = 54;
- readonly byte[] buffer;
- public BmpMaker(int width, int height)
- {
- Width = width;
- Height = height;
- int numPixels = Width * Height;
- int numPixelBytes = 4 * numPixels;
- int fileSize = headerSize + numPixelBytes;
- buffer = new byte[fileSize];
- // Escritor y acumulador de encabezados: Buffer.
- using (MemoryStream memoryStream = new MemoryStream(buffer))
- {
- //Asignación binaria del archivo temporal.
- using (BinaryWriter writer = new BinaryWriter(memoryStream, Encoding.UTF8))
- {
- // Constructor del encabezado del BitMap (14 bytes).
- writer.Write(new char[] { 'B', 'M' });
- writer.Write(fileSize);
- writer.Write((short)0);
- writer.Write((short)0);
- writer.Write(headerSize);
- // Constructor BitmapInfoHeader (40 bytes).
- writer.Write(40); // Header size
- writer.Write(Width);
- writer.Write(Height);
- writer.Write((short)1); //Tasa
- writer.Write((short)32); //Tasa
- writer.Write(0); // Compresión
- writer.Write(numPixelBytes); //Balanza de imagen en pixeles.
- writer.Write(0); //Densidad de Pixels/Color
- writer.Write(0);
- writer.Write(0);
- writer.Write(0);
- }
- }
- }
- //Instanciando Ancho y Altura
- public int Width
- {
- private set;
- get;
- }
- public int Height
- {
- private set;
- get;
- }
- //Funciones de configuración de Color y carga del Buffer
- public void SetPixel(int row, int col, Color color)
- {
- SetPixel(row, col, (int)(255 * color.R),
- (int)(255 * color.G),
- (int)(255 * color.B),
- (int)(255 * color.A));
- }
- public void SetPixel(int row, int col, int r, int g, int b, int a = 255)
- {
- int index = (row * Width + col) * 4 + headerSize;
- buffer[index + 0] = (byte)b;
- buffer[index + 1] = (byte)g;
- buffer[index + 2] = (byte)r;
- buffer[index + 3] = (byte)a;
- }
- //Método para generar la imagen.
- public ImageSource Generate()
- {
- // Creando el MemoryStream a partir del Buffer colector de bitmaps.
- MemoryStream memoryStream = new MemoryStream(buffer);
- // Conversión a StreamImageSource.
- ImageSource imageSource = ImageSource.FromStream(() =>
- {
- return memoryStream;
- });
- return imageSource;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement