private void GeneratePreview() { if (string.IsNullOrEmpty(txbImage.Text)) { return; } Bitmap vBmp = new Bitmap(txbImage.Text); picPreviewA.Image = vBmp; Bitmap vBmpResized = DrawingLogic.CopyAndResize(vBmp, Tile.WIDTH_PX * this.CreateWidth, Tile.HEIGHT_PX * this.CreateHeight); picPreviewB.Image = vBmpResized; Bitmap vBmpColorized = null; switch (cbxMethod.SelectedItem) { case METHOD_NEAREST: vBmpColorized = DrawingLogic.MapBitmapColorsToPalette((Bitmap)vBmpResized.Clone(), Palette.DEFAULT_PALETTE); break; case METHOD_FLOYDSTEINBERG: vBmpColorized = FloydSteinbergDither.Process((Bitmap)vBmpResized.Clone(), Palette.DEFAULT_PALETTE.mColors); break; } picPreviewC.Image = vBmpColorized; }
private System.Drawing.Bitmap ConvertImage(System.Drawing.Bitmap bx) { System.Drawing.Bitmap b = bx; switch (LoadOptions.Dither) { case LoadOptions.DitherFilter.FloydSteinbergDither: AddComment("Image Converted with FloydSteinbergDither"); AddComment("GrayThreshold", LoadOptions.GrayThreshold); b = new FloydSteinbergDither { GrayThreshold = LoadOptions.GrayThreshold }.Process(b); break; case LoadOptions.DitherFilter.NewspaperDither: AddComment("Image Converted with NewspaperDither"); AddComment("GrayThreshold", LoadOptions.GrayThreshold); AddComment("Dithersize", LoadOptions.NewspaperDitherSize); b = new NewspaperDither { GrayThreshold = LoadOptions.GrayThreshold, DotSize = LoadOptions.NewspaperDitherSize }.Process(b); break; } return(b); }
private void btnTilizator_Click(object sender, EventArgs e) { using (FrmTilizator vFrm = new FrmTilizator()) { if (vFrm.ShowDialog(this) == DialogResult.OK) { //Start: create target bitmap with right size Bitmap vTarget = DrawingLogic.CopyAndResize(vFrm.CreateBmp, Tile.WIDTH_PX * vFrm.CreateWidth, Tile.HEIGHT_PX * vFrm.CreateHeight); //map colors Palette vPal = Palette.DEFAULT_PALETTE; switch (vFrm.ColorMethod) { case FrmTilizator.METHOD_NEAREST: //Updates the bitmap in parameter vTarget = DrawingLogic.MapBitmapColorsToPalette(vTarget, vPal); break; case FrmTilizator.METHOD_FLOYDSTEINBERG: vTarget = FloydSteinbergDither.Process(vTarget, vPal.mColors); break; } //tilization int vTileNewCount = 0, vTileReusedCount = 0; for (int x = 0; x < vFrm.CreateWidth; x++) { for (int y = 0; y < vFrm.CreateHeight; y++) { Rectangle vTileRect = new Rectangle(x * Tile.WIDTH_PX, y * Tile.HEIGHT_PX, Tile.WIDTH_PX, Tile.HEIGHT_PX); Bitmap vTileBmp = (Bitmap)vTarget.Clone(vTileRect, vTarget.PixelFormat); //make new tile and add/get similar from library Tile vT = new Tile(vTileBmp, vPal); bool vTileAlreadyExisted = false; vT = this.mCurrentMap.ParentProject.mLibraries[0].AddTileWithoutDuplicate(vT, out vTileAlreadyExisted); if (!vTileAlreadyExisted) { vTileNewCount++; } else { vTileReusedCount++; } //apply the tile to the map this.mCurrentMap.SetTile(vT, vFrm.CreateLeft + x, vFrm.CreateTop + y); } } //refresh panMap.Invalidate(); ((FrmMain)this.FindForm()).SetStatus("Tilization: generated " + vTileNewCount + " tiles, reused " + vTileReusedCount + " tiles."); } } }
//The methods shows how to use FlexCelImgExport the "hard way", without using SaveAsImage. //For normal operation you should only need to call SaveAsImage, but you could use the code here //if you need to customize the ImgExport output, or if you need to get all the images as different files. private void CreateImg(Stream OutStream, FlexCelImgExport ImgExport, ImageFormat ImgFormat, ImageColorDepth Colors, ref TImgExportInfo ExportInfo) { TPaperDimensions pd = ImgExport.GetRealPageSize(); PixelFormat RgbPixFormat = Colors != ImageColorDepth.TrueColor ? PixelFormat.Format32bppPArgb : PixelFormat.Format24bppRgb; PixelFormat PixFormat = PixelFormat.Format1bppIndexed; switch (Colors) { case ImageColorDepth.TrueColor: PixFormat = RgbPixFormat; break; case ImageColorDepth.Color256: PixFormat = PixelFormat.Format8bppIndexed; break; } using (Bitmap OutImg = CreateBitmap(ImgExport.Resolution, pd, PixFormat)) { Bitmap ActualOutImg = Colors != ImageColorDepth.TrueColor ? CreateBitmap(ImgExport.Resolution, pd, RgbPixFormat) : OutImg; try { using (Graphics Gr = Graphics.FromImage(ActualOutImg)) { Gr.FillRectangle(Brushes.White, 0, 0, ActualOutImg.Width, ActualOutImg.Height); //Clear the background ImgExport.ExportNext(Gr, ref ExportInfo); } if (Colors == ImageColorDepth.BlackAndWhite) { FloydSteinbergDither.ConvertToBlackAndWhite(ActualOutImg, OutImg); } else if (Colors == ImageColorDepth.Color256) { OctreeQuantizer.ConvertTo256Colors(ActualOutImg, OutImg); } } finally { if (ActualOutImg != OutImg) { ActualOutImg.Dispose(); } } OutImg.Save(OutStream, ImgFormat); } }
public void FloydSteinberg2() { var dt = new FloydSteinbergDither(); const int WIDTH = 100; const int HEIGHT = 100; var b1 = new Bitmap(WIDTH, HEIGHT, PixelFormat.Format32bppArgb); b1.SetPixel(50, 50, Color.FromArgb(255, 127, 127, 127)); b1.SetPixel(50, 51, Color.FromArgb(255, 127, 127, 127)); b1.SetPixel(50, 52, Color.FromArgb(255, 127, 127, 127)); b1.SetPixel(50, 53, Color.FromArgb(255, 127, 127, 127)); var b2 = dt.Process(b1); for (int x = 0; x < WIDTH; x++) { for (int y = 0; y < HEIGHT; y++) { Color col = b2.GetPixel(x, y); if (col.R != 0) { col.R.Should().Be(255); col.G.Should().Be(255); col.B.Should().Be(255); x.Should().Be(50); y.Should().BeOneOf(53, 51); } else { col.R.Should().Be(0); col.G.Should().Be(0); col.B.Should().Be(0); } } } }
public void FloydSteinberg1() { var dt = new FloydSteinbergDither(); const int XSIZE = 100; const int YSIZE = 100; var b1 = new Bitmap(XSIZE, YSIZE); b1.SetPixel(50, 50, Color.FromArgb(255, 255, 255, 255)); var b2 = dt.Process(b1); for (int x = 0; x < XSIZE; x++) { for (int y = 0; y < YSIZE; y++) { Color col = b2.GetPixel(x, y); if (col.R != 0) { col.R.Should().Be(255); col.G.Should().Be(255); col.B.Should().Be(255); x.Should().Be(50); y.Should().Be(50); } else { col.R.Should().Be(0); col.G.Should().Be(0); col.B.Should().Be(0); } } } }