/// <summary> /// Create a FormsCardView to display the QslCard and position the view /// in the middle of the CardPanel /// </summary> /// <param name="card">CardWF object to create view of</param> public void AddCard(CardWF card) { QslCard = card; FormsCardView view = new FormsCardView(QslCard); view.Location = new Point(0, 0); view.Size = new Size(this.Width, this.Height); view.CardLocation = new Point((this.Width - QslCard.Width) / 2, (this.Height - QslCard.Height) / 2); this.Controls.Add(view); }
/// <summary> /// Saves cards with QSO info as JPEGs for photo printing /// </summary> /// <param name="startNumber">largest number (nnn) in file names QSLnnn.jpeg in print directory before printing starts</param> /// <param name="directoryName">path to directory that JPEG files are to be written to</param> /// <exception>DirectoryNotFoundException when directory has been deleted between time directory was selected and attempt /// to create or write file.</exception> /// <exception>SecurityException when user does not permission to access the directory or file (e.g. write permission on directory.</exception> /// <exception>IOException for an IO error</exception> /// <exception>PathTooLongException when the directory is longer than 248 characters, or filename is longer than 260 characters.</exception> /// <exception>Several others that definitely should never occur.</exception> private void SaveCardsAsJpegsForPrinting(int startNumber, string directoryName) { CardTabItem cti = mainTabControl.SelectedItem as CardTabItem; if(cti != null) { //create a list of QSOs for each card List<List<DispQso>> qsos = qsosView.DisplayQsos.GetDispQsosList(cti.cardPanel.QslCard); // now create file for each card for(int cardNum = 0; cardNum < qsos.Count; cardNum++) { CardWF card = cti.cardPanel.QslCard.Clone(); card.IsInDesignMode = false; FormsCardView cView = new FormsCardView(card); // inform user of progress StatusText.Text = "Creating card " + (cardNum+1) + " of " + qsos.Count; StatusText.InvalidateVisual(); ForceUIUpdate(); // create the card and render it float scale = (float)300F / 100.0F; int bitmapWidth = (int)((float)card.Width * scale); int bitmapHeight = (int)((float)card.Height * scale); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(bitmapWidth, bitmapHeight); bitmap.SetResolution(300F, 300F); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap); graphics.ScaleTransform(scale, scale); cView.PaintCard(graphics, qsos[cardNum], 96F / 300F); graphics.Dispose(); string fileName = directoryName + "\\Qsl" + (startNumber + cardNum + 1) + ".jpg"; bitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); bitmap.Dispose(); // force garbage collection to prevent Out of Memory exception if(cardNum % 50 == 0) { System.GC.WaitForPendingFinalizers(); System.GC.Collect(); } } // tell user that all cards are created StatusText.Text = qsos.Count + " of " + qsos.Count + " cards created in directory: " + directoryName; } }
/// <summary> /// Save a card as JPEG file /// </summary> /// <param name="fileName">path to the file to save</param> /// <param name="resolution">image resolution</param> /// <param name="quality">image quality</param> /// <param name="showQsos">Boolean to indicate whether the card should include QSO info</param> private void SaveCardAsJpeg(string fileName, int resolution, int quality, bool showQsos) { // create visual of the card CardTabItem cti = mainTabControl.SelectedItem as CardTabItem; if(cti != null) { CardWF card = cti.cardPanel.QslCard.Clone(); card.IsInDesignMode = false; FormsCardView cView = new FormsCardView(card); List<List<DispQso>> dispQsos = new List<List<DispQso>>(); if(card.QsosBox != null) { if(qsosView.DisplayQsos.Count > 0 && showQsos) { dispQsos = qsosView.DisplayQsos.GetDispQsosList(card); } } float scale = (float)resolution / 100.0F; int bitmapWidth = (int)((float)card.Width * scale); int bitmapHeight = (int)((float)card.Height * scale); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(bitmapWidth, bitmapHeight); bitmap.SetResolution(resolution, resolution); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap); graphics.ScaleTransform(scale, scale); cView.PaintCard(graphics, dispQsos.Count > 0 ? dispQsos[0] : null, 96F / resolution); graphics.Dispose(); System.Drawing.Imaging.ImageCodecInfo jpgEncoder = GetEncoder(System.Drawing.Imaging.ImageFormat.Jpeg); System.Drawing.Imaging.Encoder qualityEncoder = System.Drawing.Imaging.Encoder.Quality; System.Drawing.Imaging.EncoderParameter encoderParameter = new System.Drawing.Imaging.EncoderParameter(qualityEncoder, quality); System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters(1); encoderParams.Param[0] = encoderParameter; bitmap.Save(fileName, jpgEncoder, encoderParams); bitmap.Dispose(); } }
/// <summary> /// Helper method that saves 4 Up Cards in a JPEG file /// </summary> /// <param name="fileName">name of file to store image in</param> /// <param name="resolution">Resolution to store the image at</param> /// <param name="incBorders">Boolean indicating whether card edges should be included</param> private void Save4UpAsJpeg(string fileName, float resolution, bool incBorders) { // create visual of the card // create the card and render it CardTabItem cti = mainTabControl.SelectedItem as CardTabItem; CardWF cardToSave = cti.cardPanel.QslCard; float scale = resolution / 100.0F; int bitmapWidth = (int)((float)cardToSave.Width * scale * 2); int bitmapHeight = (int)((float)cardToSave.Height * scale * 2); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(bitmapWidth, bitmapHeight); bitmap.SetResolution(resolution, resolution); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap); graphics.ScaleTransform(scale, scale); for (int cardNo = 0; cardNo <= 3; cardNo++) { CardWF card = cardToSave.Clone(); card.IsInDesignMode = false; card.CardPrintProperties.PrintCardOutlines = incBorders; FormsCardView cView = new FormsCardView(card); System.Drawing.Drawing2D.GraphicsState state = graphics.Save(); float offX = (cardNo % 2 == 0) ? 0 : card.Width; float offY = (cardNo < 2) ? 0 : card.Height; graphics.TranslateTransform(offX, offY); cView.PaintCard(graphics, null, 96F / resolution); graphics.Restore(state); } graphics.Dispose(); bitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); bitmap.Dispose(); }
/// <summary> /// Handler for PrintPage event /// </summary> /// <param name="e">PrintPageEventArgs</param> protected override void OnPrintPage(PrintPageEventArgs e) { base.OnPrintPage(e); Graphics g = e.Graphics; float hardX = this.PrinterSettings.DefaultPageSettings.HardMarginX; float hardY = this.PrinterSettings.DefaultPageSettings.HardMarginY; int cardsPerPage = cardsWide * cardsHigh; for(int hCards = 0; hCards < cardsWide; hCards++) { for(int vCards = 0; vCards < cardsHigh; vCards++) { if(cardsPrinted < dispQsos.Count || PrintProperties.FillLastPage) { CardWF card = QslCard.Clone(); card.CardPrintProperties = this.PrintProperties; card.IsInDesignMode = false; FormsCardView view = new FormsCardView(card); System.Drawing.Drawing2D.GraphicsState gState = g.Save(); float x = leftOffset + hCards * card.Width - hardX; float y = topOffset + vCards * card.Height - hardY; if(App.Logger.DebugPrinting) { App.Logger.Log("Printing card at " + x + ", " + y + Environment.NewLine); } g.TranslateTransform(x, y); List<DispQso> qs = null; if(cardsPrinted < dispQsos.Count) { qs = dispQsos[cardsPrinted]; } view.PaintCard(g, qs); g.Restore(gState); cardsPrinted++; } } } e.HasMorePages = cardsPrinted < dispQsos.Count; }