public static Bitmap RenderToBitmap(int nTwipSize, ArrayList commandList, ArrayList fillStyles, ArrayList lineStyles, out EPoint ptOffset) { EPoint pntCurrentLoc = new EPoint(); ArrayList pathInfos = new ArrayList(); PathInfo pathInfoCurrent = new PathInfo(); pathInfos.Add(pathInfoCurrent); bool hasDrawnSinceLastStyleChange = false; EPointF ptScale = new EPointF(1, 1) / nTwipSize; string sDebug = ""; foreach (ShapeCommand.Base cmd in commandList) { if (cmd.MovesTurtle) { if (cmd.Draws) { ((ShapeCommand.Draw)cmd).AddToPath(pathInfoCurrent.Path, pntCurrentLoc, ptScale.X); if (Shape.Debug) { ArrayList pts = ((ShapeCommand.Draw)cmd).GeneratePoints(pntCurrentLoc); if (cmd is ShapeCommand.Curve) { sDebug += "Curve"; } else { sDebug += "Line"; } sDebug += "\r\n"; foreach (EPointF pt in pts) { sDebug += pt.ToString() + "\r\n"; } } hasDrawnSinceLastStyleChange = true; } else { pathInfoCurrent.Path.CloseAllFigures(); pathInfoCurrent.Path.StartFigure(); } pntCurrentLoc = cmd.GetNewLoc(pntCurrentLoc); } else { if (hasDrawnSinceLastStyleChange) { pathInfoCurrent = new PathInfo(); pathInfos.Add(pathInfoCurrent); } if (cmd is ShapeCommand.FillStyle) { ShapeCommand.FillStyle fs = (ShapeCommand.FillStyle)cmd; Brush brush = null; if (fs.StyleId > 0) { brush = ((Style.FillStyle)fillStyles[fs.StyleId - 1]).GetBrush(); } if (fs.Side == 0) { pathInfoCurrent.Brush0 = brush; } else { pathInfoCurrent.Brush1 = brush; } } else if (cmd is ShapeCommand.LineStyle) { ShapeCommand.LineStyle ls = (ShapeCommand.LineStyle)cmd; Pen pen = null; if (ls.StyleId > 0) { pen = ((Style.LineStyle)lineStyles[ls.StyleId - 1]).GetPen(); } pathInfoCurrent.Pen = pen; if (pen != null) { pen.Width *= ptScale.X; } } hasDrawnSinceLastStyleChange = false; } } Matrix transform = new Matrix(); transform.Scale(ptScale.X, ptScale.Y); ERectangle bounds = ERectangle.FromLTRB(99999, 99999, -99999, -99999); foreach (PathInfo pathInfo in pathInfos) { //pathInfo.Path.Transform(transform); bounds.Expand(pathInfo.GetBounds().ToERectangle()); } ptOffset = bounds.TopLeft; if (bounds.Width == 0 || bounds.Height == 0) { return(null); } //this.Bounds = bounds; if (Shape.Debug) { Endogine.Files.FileReadWrite.Write("__s.txt", sDebug); } transform = new Matrix(); transform.Translate(-bounds.X, -bounds.Y); foreach (PathInfo pathInfo in pathInfos) { pathInfo.Path.Transform(transform); } Bitmap bmp = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb); Endogine.BitmapHelpers.Canvas canvas = Endogine.BitmapHelpers.Canvas.Create(bmp); canvas.Locked = true; canvas.Fill(Color.FromArgb(0, 255, 255, 255)); canvas.Dispose(); Graphics g = Graphics.FromImage(bmp); g.SmoothingMode = SmoothingMode.HighQuality; foreach (PathInfo pathInfo in pathInfos) { if (pathInfo.Pen != null) { g.DrawPath(pathInfo.Pen, pathInfo.Path); } if (pathInfo.Brush0 != null) { g.FillPath(pathInfo.Brush0, pathInfo.Path); } if (pathInfo.Brush1 != null) //TODO: can GDI+ handle Flash's two different fills (0 and 1)? { g.FillPath(pathInfo.Brush1, pathInfo.Path); } } return(bmp); }
public static Bitmap PackBitmapsIntoOneLarge(ArrayList bmps, EPoint pntPreferredLayout, out Node infoRoot) { //If no specification of number of tiles on X and Y, make a guess: if (pntPreferredLayout == null) { int nNumOnX = (int)Math.Sqrt(bmps.Count); if (nNumOnX * nNumOnX < bmps.Count) { nNumOnX++; } int nNumOnY = bmps.Count / nNumOnX; if (nNumOnX * nNumOnY < bmps.Count) { nNumOnY++; } pntPreferredLayout = new EPoint(nNumOnX, nNumOnY); } bool bTrimWhiteSpace = true; //when packing the bitmap tightly, must store the offsets of bitmap frames so they don't wiggle on playback: bool bUseIndividualOffsets = true; infoRoot = new Node(); Node node = infoRoot.AppendChild("root"); node.AppendChild("NumFramesTotal").Value = bmps.Count; node.AppendChild("NumFramesOnX").Value = pntPreferredLayout.X; Node subNode; subNode = node.AppendChild("Animations"); //subNode.AppendChild("Default").Value = "0 0-4"; EPoint[] offsets = new EPoint[bmps.Count]; //this is the smallest rectangle that can encompass all frames: ERectangle rctBounds = ERectangle.FromLTRB(9999, 9999, -9999, -9999); //Trim white space from all bitmaps subNode = node.AppendChild("Frames"); for (int i = 0; i < bmps.Count; i++) { Bitmap bmp = (Bitmap)bmps[i]; if (bTrimWhiteSpace) { EPoint pntMid = new EPoint(bmp.Size.Width, bmp.Size.Height) / 2; EPoint pntTopLeftCorner; bmp = BitmapHelper.TrimWhitespace(bmp, out pntTopLeftCorner); bmps[i] = bmp; offsets[i] = pntTopLeftCorner; if (bUseIndividualOffsets) //make more compact (but offset values are needed): { rctBounds.Expand(new ERectangle(0, 0, bmp.Width, bmp.Height)); //make offset to middle of input bitmap offsets[i] = pntMid - pntTopLeftCorner; } else //Expand bounds so no offset values are needed: { rctBounds.Expand(new ERectangle(pntTopLeftCorner.X, pntTopLeftCorner.Y, bmp.Width, bmp.Height)); } } else { rctBounds.Expand(new ERectangle(0, 0, bmp.Width, bmp.Height)); } } //Create the merged bitmap: EPoint totalSize = rctBounds.Size * pntPreferredLayout; Bitmap largeBmp = new Bitmap(totalSize.X, totalSize.Y); Graphics g = Graphics.FromImage(largeBmp); for (int i = 0; i < bmps.Count; i++) { Bitmap bmp = (Bitmap)bmps[i]; EPoint pntDst = new EPoint((i % pntPreferredLayout.X) * rctBounds.Size.X, (i / pntPreferredLayout.X) * rctBounds.Size.Y); Node subsub = subNode.AppendChild("Frame"); subsub.Value = i.ToString(); if (bUseIndividualOffsets) { //subsub.AppendChild("Offset").Value = (offsets[i]+rctBounds.TopLeft).ToString(); subsub.AppendChild("Offset").Value = offsets[i].ToString(); } else { //pntDst is upper left corner of destination rectangle. //Since we don't use individual offset, we want to move it according to offset: pntDst = pntDst - rctBounds.TopLeft + offsets[i]; } g.DrawImage(bmp, new RectangleF(pntDst.ToPoint(), new Size(bmp.Width, bmp.Height))); } if (bUseIndividualOffsets) { node.AppendChild("RegPoint").Value = new EPoint().ToString(); } else { node.AppendChild("RegPoint").Value = rctBounds.TopLeft.ToString(); } return(largeBmp); }
public static void MaskFiles(string filesearch, Bitmap mask24Bit, string outputPath, EPoint offset) { if (offset == null) { offset = new EPoint(); } Bitmap bmpSrc = mask24Bit; Graphics g = null; Bitmap bmpA = null; EPoint pntNewSize = new EPoint(); System.IO.FileInfo[] files = Endogine.Files.FileFinder.GetFiles(filesearch); foreach (System.IO.FileInfo fi in files) { Bitmap bmpDst = new Bitmap(fi.FullName); //Find the rect that encloses both bitmaps ERectangle rctCommon = new ERectangle(0, 0, bmpSrc.Width, bmpSrc.Height); rctCommon.Offset(offset); rctCommon.Expand(new ERectangle(0, 0, bmpDst.Width, bmpDst.Height)); //and make both bitmaps the same size (without scaling) Bitmap bmpX; EPoint pnt; bmpX = new Bitmap(bmpDst, rctCommon.Size.ToSize()); g = Graphics.FromImage(bmpX); //g.FillRectangle(new SolidBrush(Color.FromArgb(255,255,255,255)), 0,0,bmpX.Width,bmpX.Height); g.Clear(Color.FromArgb(0, 0, 0, 0)); pnt = new EPoint(); if (offset.X < 0) { pnt.X = -offset.X; } if (offset.Y < 0) { pnt.Y = -offset.Y; } g.DrawImage(bmpDst, new Rectangle(pnt.X, pnt.Y, bmpDst.Width, bmpDst.Height), 0, 0, bmpDst.Width, bmpDst.Height, GraphicsUnit.Pixel); //g.DrawImageUnscaled(bmpDst, pnt.X,pnt.Y, bmpDst.Width, bmpDst.Height); //(bmpX.Width-bmpDst.Width)/2, (bmpX.Height-bmpDst.Height)/2); bmpDst = bmpX; bmpX = new Bitmap(bmpSrc, rctCommon.Size.ToSize()); g = Graphics.FromImage(bmpX); g.FillRectangle(new SolidBrush(Color.Black), 0, 0, bmpX.Width, bmpX.Height); pnt = new EPoint(); if (offset.X > 0) { pnt.X = offset.X; } if (offset.Y > 0) { pnt.Y = offset.Y; } g.DrawImage(bmpSrc, new Rectangle(pnt.X, pnt.Y, bmpSrc.Width, bmpSrc.Height), 0, 0, bmpSrc.Width, bmpSrc.Height, GraphicsUnit.Pixel); //g.DrawImageUnscaled(bmpSrc, pnt.X,pnt.Y); //(bmpX.Width-bmpSrc.Width)/2, (bmpX.Height-bmpSrc.Height)/2); bmpSrc = bmpX; if (true) { //this will be done the first time, and each time the bmpDst.Size is different from the last //with the resizing above, this will only happen once if (bmpDst.Size != pntNewSize.ToSize()) { pntNewSize = new EPoint(bmpDst.Size.Width, bmpDst.Size.Height); bmpA = new Bitmap(bmpDst.Size.Width, bmpDst.Size.Height, PixelFormat.Format24bppRgb); g = Graphics.FromImage(bmpA); g.DrawImage(bmpSrc, new Rectangle(0, 0, bmpDst.Width, bmpDst.Height), new Rectangle(0, 0, bmpSrc.Width, bmpSrc.Height), GraphicsUnit.Pixel); bmpA = Endogine.BitmapHelpers.BitmapHelper.ExtractChannel(bmpA, 0); //bmpA.Save(fi.DirectoryName+"\\a__maskX"+fi.Name); } } Endogine.BitmapHelpers.BitmapHelper.Mask(bmpDst, bmpA); string sOut = outputPath + fi.Name; bmpDst.Save(sOut); } }
public static void MaskFiles(string filesearch, Bitmap mask24Bit, string outputPath, EPoint offset) { if (offset == null) offset = new EPoint(); Bitmap bmpSrc = mask24Bit; Graphics g = null; Bitmap bmpA = null; EPoint pntNewSize = new EPoint(); System.IO.FileInfo[] files = Endogine.Files.FileFinder.GetFiles(filesearch); foreach (System.IO.FileInfo fi in files) { Bitmap bmpDst = new Bitmap(fi.FullName); //Find the rect that encloses both bitmaps ERectangle rctCommon = new ERectangle(0,0,bmpSrc.Width,bmpSrc.Height); rctCommon.Offset(offset); rctCommon.Expand(new ERectangle(0,0,bmpDst.Width,bmpDst.Height)); //and make both bitmaps the same size (without scaling) Bitmap bmpX; EPoint pnt; bmpX = new Bitmap(bmpDst, rctCommon.Size.ToSize()); g = Graphics.FromImage(bmpX); //g.FillRectangle(new SolidBrush(Color.FromArgb(255,255,255,255)), 0,0,bmpX.Width,bmpX.Height); g.Clear(Color.FromArgb(0,0,0,0)); pnt = new EPoint(); if (offset.X < 0) pnt.X = -offset.X; if (offset.Y < 0) pnt.Y = -offset.Y; g.DrawImage(bmpDst, new Rectangle(pnt.X,pnt.Y, bmpDst.Width, bmpDst.Height), 0,0,bmpDst.Width, bmpDst.Height, GraphicsUnit.Pixel); //g.DrawImageUnscaled(bmpDst, pnt.X,pnt.Y, bmpDst.Width, bmpDst.Height); //(bmpX.Width-bmpDst.Width)/2, (bmpX.Height-bmpDst.Height)/2); bmpDst = bmpX; bmpX = new Bitmap(bmpSrc, rctCommon.Size.ToSize()); g = Graphics.FromImage(bmpX); g.FillRectangle(new SolidBrush(Color.Black), 0,0, bmpX.Width,bmpX.Height); pnt = new EPoint(); if (offset.X > 0) pnt.X = offset.X; if (offset.Y > 0) pnt.Y = offset.Y; g.DrawImage(bmpSrc, new Rectangle(pnt.X,pnt.Y, bmpSrc.Width, bmpSrc.Height), 0,0,bmpSrc.Width, bmpSrc.Height, GraphicsUnit.Pixel); //g.DrawImageUnscaled(bmpSrc, pnt.X,pnt.Y); //(bmpX.Width-bmpSrc.Width)/2, (bmpX.Height-bmpSrc.Height)/2); bmpSrc = bmpX; if (true) { //this will be done the first time, and each time the bmpDst.Size is different from the last //with the resizing above, this will only happen once if (bmpDst.Size != pntNewSize.ToSize()) { pntNewSize = new EPoint(bmpDst.Size.Width,bmpDst.Size.Height); bmpA = new Bitmap(bmpDst.Size.Width,bmpDst.Size.Height, PixelFormat.Format24bppRgb); g = Graphics.FromImage(bmpA); g.DrawImage(bmpSrc, new Rectangle(0,0,bmpDst.Width,bmpDst.Height), new Rectangle(0,0,bmpSrc.Width,bmpSrc.Height), GraphicsUnit.Pixel); bmpA = Endogine.BitmapHelpers.BitmapHelper.ExtractChannel(bmpA,0); //bmpA.Save(fi.DirectoryName+"\\a__maskX"+fi.Name); } } Endogine.BitmapHelpers.BitmapHelper.Mask(bmpDst, bmpA); string sOut = outputPath+fi.Name; bmpDst.Save(sOut); } }