public void RestoreCanvasScale(HCCanvas aCanvas, ScaleInfo aOldInfo) { POINT pt = new POINT(); SIZE size = new SIZE(); GDI.SetViewportOrgEx(aCanvas.Handle, aOldInfo.ViewportOrg.X, aOldInfo.ViewportOrg.Y, ref pt); GDI.SetViewportExtEx(aCanvas.Handle, aOldInfo.ViewportExt.cx, aOldInfo.ViewportExt.cy, ref size); GDI.SetWindowOrgEx(aCanvas.Handle, aOldInfo.WindowOrg.X, aOldInfo.WindowOrg.Y, ref pt); GDI.SetWindowExtEx(aCanvas.Handle, aOldInfo.WindowExt.cx, aOldInfo.WindowExt.cy, ref size); GDI.SetMapMode(aCanvas.Handle, aOldInfo.MapMode); }
public ScaleInfo ScaleCanvas(HCCanvas aCanvas) { ScaleInfo Result = new ScaleInfo(); Result.MapMode = GDI.GetMapMode(aCanvas.Handle); // 返回映射方式,零则失败 GDI.SetMapMode(aCanvas.Handle, GDI.MM_ANISOTROPIC); // 逻辑单位转换成具有任意比例轴的任意单位,用SetWindowsEx和SetViewportExtEx函数指定单位、方向和需要的比例 GDI.SetWindowOrgEx(aCanvas.Handle, 0, 0, ref Result.WindowOrg); // 用指定的坐标设置设备环境的窗口原点 GDI.SetWindowExtEx(aCanvas.Handle, FWindowWidth, FWindowHeight, ref Result.WindowExt); // 为设备环境设置窗口的水平的和垂直的范围 GDI.SetViewportOrgEx(aCanvas.Handle, 0, 0, ref Result.ViewportOrg); // 哪个设备点映射到窗口原点(0,0) // 用指定的值来设置指定设备环境坐标的X轴、Y轴范围 GDI.SetViewportExtEx(aCanvas.Handle, (int)Math.Round(FWindowWidth * FScaleX), (int)Math.Round(FWindowHeight * FScaleY), ref Result.ViewportExt); return(Result); }
public void SaveToImage(string path, string prefix, string imageType, bool onePaper = true) { HCCanvas vBmpCanvas = new HCCanvas(); SectionPaintInfo vPaintInfo = new SectionPaintInfo(); try { vPaintInfo.ScaleX = 1; vPaintInfo.ScaleY = 1; vPaintInfo.Zoom = 1; vPaintInfo.Print = true; vPaintInfo.DPI = HCUnitConversion.PixelsPerInchX; vPaintInfo.ViewModel = HCViewModel.hvmFilm; int vWidth = 0, vHeight = 0; if (onePaper) { for (int i = 0; i < FSections.Count; i++) { vHeight = vHeight + FSections[i].PaperHeightPix * FSections[i].PageCount; if (vWidth < FSections[i].PaperWidthPix) { vWidth = FSections[i].PaperWidthPix; } } vPaintInfo.WindowWidth = vWidth; vPaintInfo.WindowHeight = vHeight; using (Bitmap vBmp = new Bitmap(vWidth, vHeight)) { vBmpCanvas.Graphics = Graphics.FromImage(vBmp); int vSectionIndex = 0, vSectionPageIndex = 0, vTop = 0; for (int i = 0; i < this.PageCount; i++) { vSectionIndex = GetSectionPageIndexByPageIndex(i, ref vSectionPageIndex); //vWidth = FSections[vSectionIndex].PaperWidthPix; vHeight = FSections[vSectionIndex].PaperHeightPix; vBmpCanvas.Brush.Color = Color.White; vBmpCanvas.FillRect(new RECT(0, vTop, vWidth, vTop + vHeight)); ScaleInfo vScaleInfo = vPaintInfo.ScaleCanvas(vBmpCanvas); try { FSections[vSectionIndex].PaintPaper(vSectionPageIndex, 0, vTop, vBmpCanvas, vPaintInfo); vTop = vTop + vHeight; } finally { vPaintInfo.RestoreCanvasScale(vBmpCanvas, vScaleInfo); } } vBmpCanvas.Dispose(); if (imageType == "BMP") { vBmp.Save(path + prefix + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp); } else if (imageType == "JPG") { vBmp.Save(path + prefix + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } else { vBmp.Save(path + prefix + ".png", System.Drawing.Imaging.ImageFormat.Png); } } } else { int vSectionIndex = 0, vSectionPageIndex = 0; for (int i = 0; i < this.PageCount; i++) { vSectionIndex = GetSectionPageIndexByPageIndex(i, ref vSectionPageIndex); using (Bitmap vBmp = new Bitmap(FSections[vSectionIndex].PaperWidthPix, FSections[vSectionIndex].PaperHeightPix)) { vBmpCanvas.Graphics = Graphics.FromImage(vBmp); vBmpCanvas.Brush.Color = Color.White; vBmpCanvas.FillRect(new RECT(0, 0, vBmp.Width, vBmp.Height)); vPaintInfo.WindowWidth = vBmp.Width; vPaintInfo.WindowHeight = vBmp.Height; ScaleInfo vScaleInfo = vPaintInfo.ScaleCanvas(vBmpCanvas); try { vBmpCanvas.Brush.Color = Color.White; vBmpCanvas.FillRect(new RECT(0, 0, vBmp.Width, vBmp.Height)); FSections[vSectionIndex].PaintPaper(vSectionPageIndex, 0, 0, vBmpCanvas, vPaintInfo); } finally { vPaintInfo.RestoreCanvasScale(vBmpCanvas, vScaleInfo); } vBmpCanvas.Dispose(); if (imageType == "BMP") { vBmp.Save(path + prefix + (i + 1).ToString() + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp); } else if (imageType == "JPG") { vBmp.Save(path + prefix + (i + 1).ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } else { vBmp.Save(path + prefix + (i + 1).ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png); } } } } } finally { vPaintInfo.Dispose(); } }