public void GetPrintArea(Panel pnl) { int pnlWidth = pnl.Width; int pnlHeight = pnl.Height; MemoryImage = new Bitmap(pnlWidth, pnlHeight); Rectangle rect = new Rectangle(0, 0, pnlWidth, pnlHeight); pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnlWidth, pnlHeight)); }
public Bitmap GetCapturedImage(Panel panel, string filePath, Boolean firstTime) { Rectangle rectangle; Bitmap bitmap = null; ArrayList controls = null; try { rectangle = panel.RectangleToScreen(panel.Bounds); bitmap = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppArgb); if (firstTime) { panel.DrawToBitmap(bitmap, panel.Bounds); // 再帰的にコンテナ及びコントロールをキャプチャ controls = GetAllControls(panel); controls.Reverse(); // 背面から foreach (Control c in controls) { Rectangle rectangle2 = c.Bounds; Control control = c; while (control.Bounds.Location != panel.Bounds.Location) { rectangle2.X += control.Parent.Bounds.Location.X; rectangle2.Y += control.Parent.Bounds.Location.Y; control = control.Parent; } c.DrawToBitmap(bitmap, rectangle2); } } else { CaptureControls(panel, ref bitmap); } bitmap.Save(filePath, ImageFormat.Bmp); // 保存する場合 } catch (SystemException ex) { Console.WriteLine(ex.Message); } return bitmap; }
public void SaveBitmap(System.Windows.Forms.Panel CtrlToSave, string fileName) { Point oldPosition = new Point(this.HorizontalScroll.Value, this.VerticalScroll.Value); CtrlToSave.PerformLayout(); ComposedImage ci = new ComposedImage(new Size(CtrlToSave.DisplayRectangle.Width, CtrlToSave.DisplayRectangle.Height)); int visibleWidth = CtrlToSave.Width - (CtrlToSave.VerticalScroll.Visible ? SystemInformation.VerticalScrollBarWidth : 0); int visibleHeightBuffer = CtrlToSave.Height - (CtrlToSave.HorizontalScroll.Visible ? SystemInformation.HorizontalScrollBarHeight : 0); //int Iteration = 0; for (int x = CtrlToSave.DisplayRectangle.Width - visibleWidth; x >= 0; x -= visibleWidth) { int visibleHeight = visibleHeightBuffer; for (int y = CtrlToSave.DisplayRectangle.Height - visibleHeight; y >= 0; y -= visibleHeight) { CtrlToSave.HorizontalScroll.Value = x; CtrlToSave.VerticalScroll.Value = y; CtrlToSave.PerformLayout(); Bitmap bmp = new Bitmap(visibleWidth, visibleHeight); CtrlToSave.DrawToBitmap(bmp, new Rectangle(0, 0, visibleWidth, visibleHeight)); ci.images.Add(new ImagePart(new Point(x, y), bmp)); ///Show image parts //using (Graphics grD = Graphics.FromImage(bmp)) //{ // Iteration++; // grD.DrawRectangle(Pens.Blue,new Rectangle(0,0,bmp.Width-1,bmp.Height-1)); //grD.DrawString("x:"+x+",y:"+y+",W:"+visibleWidth+",H:"+visibleHeight + " I:" + Iteration,new Font("Segoe UI",9f),Brushes.Red,new Point(2,2)); //} if (y - visibleHeight < (CtrlToSave.DisplayRectangle.Height % visibleHeight)) { visibleHeight = CtrlToSave.DisplayRectangle.Height % visibleHeight; } if (visibleHeight == 0) { break; } } if (x - visibleWidth < (CtrlToSave.DisplayRectangle.Width % visibleWidth)) { visibleWidth = CtrlToSave.DisplayRectangle.Width % visibleWidth; } if (visibleWidth == 0) { break; } } Bitmap img = ci.composeImage(); img.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp); CtrlToSave.HorizontalScroll.Value = oldPosition.X; CtrlToSave.VerticalScroll.Value = oldPosition.Y; }
private bool ProcessPrintRequest(int pageNumberStart, int pageNumberEnd, bool showData) { bool isLandscape = false; bool exitPrint = false; try { List<Page> allPages = view.GetMetadata().GetViewPages(view); List<Page> pages = new List<Page>(); pages = allPages.GetRange(pageNumberStart - 1, 1 + pageNumberEnd - pageNumberStart); foreach (Page page in pages) { printDocument = new System.Drawing.Printing.PrintDocument(); printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage); DataRow row = page.GetMetadata().GetPageSetupData(view); isLandscape = row["Orientation"].ToString().ToLower() == "landscape"; Panel printPanel = new Panel(); float dpiX; Graphics graphics = printPanel.CreateGraphics(); dpiX = graphics.DpiX; int height = (int)row["Height"]; int width = (int)row["Width"]; if (dpiX != 96) { float scaleFactor = (dpiX * 1.041666666f) / 100; height = Convert.ToInt32(((float)height) * (float)scaleFactor); width = Convert.ToInt32(((float)width) * (float)scaleFactor); } if (isLandscape) { printPanel.Size = new System.Drawing.Size(height, width); printDocument.DefaultPageSettings.Landscape = true; } else { printPanel.Size = new System.Drawing.Size(width, height); } ControlFactory factory = ControlFactory.Instance; List<Control> pageControls = factory.GetPageControls(page, printPanel.Size); SortedList<FieldGroupBox, System.Drawing.Point> groupsOnPage = new SortedList<FieldGroupBox, System.Drawing.Point>(new GroupZeeOrderComparer()); List<Control> childrenOnPage = new List<Control>(); List<Control> orphansOnPage = new List<Control>(); ArrayList namesOfChildenOnPage = new ArrayList(); Panel panel = _fieldPanel; foreach (Control control in pageControls) { Control printControl = control; Field field = factory.GetAssociatedField(control); field = this.view.Fields[field.Name]; if (control is DragableGroupBox == false) { try { if (control is RichTextBox) { printControl = new TextBox(); ((TextBoxBase)printControl).BorderStyle = ((TextBoxBase)control).BorderStyle; ((TextBoxBase)printControl).Multiline = true; printControl.Left = control.Left; printControl.Top = control.Top; printControl.Height = control.Height; printControl.Width = control.Width; printControl.Font = control.Font; } printPanel.Controls.Add(printControl); } catch { return false; } } if (printControl is Label) continue; if (printControl is FieldGroupBox) continue; if (showData) { if (field is ImageField) { this.canvas.GetFieldDataIntoControl(field as ImageField, printControl as PictureBox); } else if (field is YesNoField) { this.canvas.GetFieldDataIntoControl(field as YesNoField, printControl as ComboBox); if (((ComboBox)printControl).SelectedValue == null) { foreach (Control panelControl in panel.Controls) { if (panelControl is Label) continue; if (panelControl is FieldGroupBox) continue; Field panelfield = factory.GetAssociatedField(panelControl); panelfield = this.view.Fields[field.Name]; if (panelfield.Name == field.Name) { string holdit = ((YesNoField)panelfield).CurrentRecordValueString.ToString(); string yes = "1"; string no = "0"; if (holdit.Contains(yes)) { printControl.Text = "Yes"; } else if (holdit.Contains(no)) { printControl.Text = "No"; } } } } } else if (printControl is TextBox || printControl is ComboBox || printControl is CheckBox || printControl is MaskedTextBox || printControl is DataGridView || printControl is GroupBox || printControl is DateTimePicker) { if (field is MirrorField) { this.canvas.GetMirrorData(field as MirrorField, printControl); } else if (field is IDataField || printControl is DataGridView) { if (printControl is TextBox) { this.canvas.GetTextData(field, printControl); } else if (printControl is RichTextBox) { this.canvas.GetTextData(field, printControl); } else if (field is DateField) { this.canvas.GetDateData(field as DateField, printControl); } else if (field is TimeField) { this.canvas.GetTimeData(field as TimeField, printControl); } else if (field is DateTimeField) { this.canvas.GetDateTimeData(field as DateTimeField, printControl); } else if (printControl is MaskedTextBox) { if (((IDataField)field).CurrentRecordValueObject != null) { if (((MaskedTextBox)printControl).Mask != null) { if (field is NumberField && !Util.IsEmpty(((IDataField)field).CurrentRecordValueString)) { printControl.Text = FormatNumberInput(((NumberField)field).CurrentRecordValueString, ((NumberField)field).Pattern); } else { printControl.Text = ((IDataField)field).CurrentRecordValueString; } } else { printControl.Text = ((IDataField)field).CurrentRecordValueString; } } else { printControl.Text = string.Empty; } } else if (printControl is ComboBox) { this.canvas.GetComboboxData(field, printControl); } else if (printControl is CheckBox) { this.canvas.GetCheckBoxData(field, printControl); } else if (printControl is DataGridView) { this.canvas.GetDataGridViewData(field, printControl); } else if (field is OptionField) { this.canvas.GetOptionData((OptionField)field, printControl); } } } } else { if (printControl is TextBox || printControl is ComboBox) { if (field is IDataField || printControl is DataGridView) { if (printControl is ComboBox) { ((ComboBox)printControl).Text = ""; } } if (printControl is TextBox) { ((TextBox)printControl).Text = ""; ((TextBox)printControl).Enabled = true; } } } } if (exitPrint) { break; } int colorValue; DataTable backgroundTable = page.GetMetadata().GetPageBackgroundData(page); DataRow[] rows = backgroundTable.Select("BackgroundId=" + page.BackgroundId); string imageLayout = string.Empty; if (rows.Length > 0) { imageLayout = Convert.ToString(rows[0]["ImageLayout"]); } Color color; Image image; Bitmap bufferBitmap = null; if (page != null) { if (rows.Length > 0) { colorValue = Convert.ToInt32(rows[0]["Color"]); if (rows[0]["Image"] != System.DBNull.Value) { try { byte[] imageBytes = ((byte[])rows[0]["Image"]); using (MemoryStream memStream = new MemoryStream(imageBytes.Length)) { memStream.Seek(0, SeekOrigin.Begin); memStream.Write(imageBytes, 0, imageBytes.Length); memStream.Seek(0, SeekOrigin.Begin); image = Image.FromStream(((Stream)memStream)); } } catch { image = null; } } else { image = null; } if (colorValue == 0) { color = SystemColors.Window; } else { color = Color.FromArgb(colorValue); } } else { image = null; imageLayout = "None"; color = SystemColors.Window; } } else { image = null; imageLayout = "None"; color = SystemColors.Window; } if ((image == null) && (color.Equals(Color.Empty))) { printPanel.BackColor = Color.White; } else { /* if (showtab) { foreach (Control control in pageControls) { Field field = factory.GetAssociatedField(control); if (control.TabStop == true) { bool isInputField = ((Control)control) is PairedLabel == false && field.FieldType != MetaFieldType.Group; bool isLabelField = field.FieldType == MetaFieldType.LabelTitle; if (isInputField || isLabelField) { Label lbTabSquare = new Label(); lbTabSquare.BackColor = control.TabStop ? Color.Black : Color.Firebrick; lbTabSquare.Padding = new Padding(2); lbTabSquare.ForeColor = Color.White; lbTabSquare.BorderStyle = BorderStyle.None; lbTabSquare.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold); lbTabSquare.Text = control.TabIndex.ToString() + " " + field.Name; lbTabSquare.Location = new Point(control.Location.X, control.Location.Y); lbTabSquare.Size = TextRenderer.MeasureText(lbTabSquare.Text, lbTabSquare.Font); lbTabSquare.Size = new Size(lbTabSquare.Size.Width + lbTabSquare.Padding.Size.Width, lbTabSquare.Size.Height + lbTabSquare.Padding.Size.Height); lbTabSquare.Tag = "showtab"; lbTabSquare.BringToFront(); printPanel.Controls.Add(lbTabSquare); } } } }*/ printPanel.BackgroundImageLayout = ImageLayout.None; if (printPanel.Size.Width > 0 && printPanel.Size.Height > 0) { try { Bitmap b = new Bitmap(printPanel.Size.Width, printPanel.Size.Height); Graphics bufferGraphics = Graphics.FromImage(b); if (!(color.Equals(Color.Empty))) { printPanel.BackColor = color; } bufferGraphics.Clear(printPanel.BackColor); if (image != null) { Image img = image; switch (imageLayout.ToUpper()) { case "TILE": TextureBrush tileBrush = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Tile); bufferGraphics.FillRectangle(tileBrush, 0, 0, printPanel.Size.Width, printPanel.Size.Height); tileBrush.Dispose(); break; case "STRETCH": bufferGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; bufferGraphics.DrawImage(img, 0, 0, printPanel.Size.Width, printPanel.Size.Height); bufferGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default; break; case "CENTER": int centerX = (printPanel.Size.Width / 2) - (img.Size.Width / 2); int centerY = (printPanel.Size.Height / 2) - (img.Size.Height / 2); bufferGraphics.DrawImage(img, centerX, centerY); break; default: bufferGraphics.DrawImage(img, 0, 0); break; } } foreach (Control control in pageControls) { if (control is DragableGroupBox) { Pen pen = new Pen(Color.Black); Point ul = control.Location; Point ur = new Point(control.Location.X + control.Width, control.Location.Y); Point ll = new Point(control.Location.X, control.Location.Y + control.Height); Point lr = new Point(control.Location.X + control.Width, control.Location.Y + control.Height); bufferGraphics.DrawLine(pen, ul, ur); bufferGraphics.DrawLine(pen, ur, lr); bufferGraphics.DrawLine(pen, lr, ll); bufferGraphics.DrawLine(pen, ll, ul); } } bufferGraphics.DrawImage(b, 0, 0); bufferBitmap = b; printPanel.BackgroundImage = b; } catch { bufferBitmap.Dispose(); graphics.Dispose(); printPanel.Dispose(); pageImageList = null; return false; } } } foreach (Control control in pageControls) { if (control is DragableGroupBox) { Label label = new Label(); label.Width = control.Width; label.Text = control.Text; Size textSize = TextRenderer.MeasureText(graphics, label.Text, label.Font); label.Left = control.Left + 12; label.Top = control.Top - textSize.Height / 2; label.Width = textSize.Width + 12; label.Font = control.Font; label.AutoSize = true; printPanel.Controls.Add(label); } } if (bufferBitmap != null) { graphics.DrawImage(bufferBitmap, 0, 0); } try { memoryImage = new Bitmap(printPanel.Width, printPanel.Height, graphics); printPanel.DrawToBitmap(memoryImage, new Rectangle(0, 0, printPanel.Width, printPanel.Height)); printPanel.Dispose(); pageImageList.Add(memoryImage.Clone()); } catch { bufferBitmap.Dispose(); graphics.Dispose(); printPanel.Dispose(); pageImageList = null; memoryImage.Dispose(); return false; } } } catch { memoryImage.Dispose(); pageImageList = null; return false; }; return true; }
private Bitmap makeImage(Template template) { Bitmap bmp; Panel panel = new Panel(); Panel panel0 = new Panel(); Panel panel1 = new Panel(); Label label0 = new Label(); Label label1 = new Label(); panel.Size = new Size(OBJ_WIDTH, OBJ_HEIGHT); panel.BorderStyle = BorderStyle.None; panel.BackColor = Color.Black; panel.Margin = new Padding(0, 0, 0, 0); panel.Location = new Point(0, 0); panel0.Size = new Size(OBJ_WIDTH, OBJ_HEIGHT); panel0.BorderStyle = BorderStyle.None; panel0.BackColor = Color.White; panel0.Margin = new Padding(0, 0, 0, 0); panel0.Location = new Point(0, 0); panel1.Size = new Size(OBJ_WIDTH, OBJ_HEIGHT); panel1.BorderStyle = BorderStyle.None; panel1.BackColor = Color.White; panel1.Margin = new Padding(0, 0, 0, 0); panel1.Location = new Point(0, 0); label0.Margin = new Padding(0, 0, 0, 0); label0.Padding = new Padding(0, 0, 0, 0); label0.Location = new Point(1, 1); label0.Size = new Size(10, 10); label0.BackColor = Color.White; label0.Font = new Font(FontFamily.GenericSansSerif, 6); label0.Text = "1"; label1.Margin = new Padding(0, 0, 0, 0); label1.Padding = new Padding(0, 0, 0, 0); label1.Location = new Point(1, 1); label1.Size = new Size(10, 10); label1.BackColor = Color.White; label1.Font = new Font(FontFamily.GenericSansSerif, 6); label1.Text = "2"; Panel curBmpObj; bool hasSide2 = false; foreach (TemplateObject curObj in template.objects) { curBmpObj = new Panel(); if (curObj.quizType == Constant.answerPrefix) { curBmpObj.BackColor = Color.Green; } else if (curObj.quizType == Constant.questionPrefix) { curBmpObj.BackColor = Color.Red; } else { curBmpObj.BackColor = Color.Gray; } curBmpObj.BorderStyle = BorderStyle.FixedSingle; curBmpObj.Location = new Point(Convert.ToInt32(curObj.x1 / 100.0 * OBJ_WIDTH), Convert.ToInt32(curObj.y1 / 100.0 * OBJ_HEIGHT)); curBmpObj.Size = new Size(Convert.ToInt32((curObj.x2 - curObj.x1) / 100.0 * OBJ_WIDTH), Convert.ToInt32((curObj.y2 - curObj.y1) / 100.0 * OBJ_HEIGHT)); if (curObj.side == 0) { panel0.Controls.Add(curBmpObj); } else if (curObj.side == 1) { panel1.Controls.Add(curBmpObj); hasSide2 = true; } } panel0.Controls.Add(label0); panel1.Controls.Add(label1); if (hasSide2) { panel.Height = (OBJ_HEIGHT * 2) + 1; panel1.Location = new Point(0, OBJ_HEIGHT + 1); panel.Controls.Add(panel1); } panel.Controls.Add(panel0); bmp = new Bitmap(panel.Width, panel.Height); panel.DrawToBitmap(bmp, new Rectangle(0, 0, panel.Width, panel.Height)); return bmp; }