C#中的圖像Image類與打印Printing類用法
一、Images
1、概述
Image 類為Bitmap(位圖) 和 Metafile(矢量圖) 的類提供功能的抽象基類。Image類不能直接創(chuàng)建對象的,但Image.FromFile()返回的是Bitmap或者Metafile的對象。
初始化Image:
Image img0 = Image.FromFile(@"C:\1.jpg"); Image img1 = Image.FromStream(File.OpenRead(@"C:\1.jpg")); Image img2 = new Bitmap(@"C:\1.jpg");
2、屬性
- PixelFormat:獲取此 Image 的像素格式。
- RawFormat:獲取此 Image 的文件格式。
- Size:獲取此圖像的寬度和高度(以像素為單位)。
- Width:獲取此 Image 的寬度(以像素為單位)。
- Height:獲取此 Image 的高度(以像素為單位)。
3、方法
- FromFile(String):從指定的文件創(chuàng)建 Image。
- FromStream(Stream):從指定的數(shù)據(jù)流創(chuàng)建 Image。
- GetBounds(GraphicsUnit):以指定的單位獲取圖像的界限。
- GetThumbnailImage(Int32, Int32, Image+GetThumbnailImageAbort, IntPtr):返回此 Image 的縮略圖。
- RotateFlip(RotateFlipType):旋轉、翻轉或者同時旋轉和翻轉 Image。
- Save(Stream, ImageFormat):將此圖像以指定的格式保存到指定的流中。
- Save(String, ImageFormat):將此 Image 以指定格式保存到指定文件。
4、繪制圖片:
using (Image img = new Bitmap(@"C:\1.jpg")) { System.Drawing.Graphics g = Graphics.FromImage(img); g.DrawImage(img, new System.Drawing.Rectangle(0, 0, img.Width, img.Height)); }
5、縮放:
Image img1 = new Bitmap(@"C:\1.jpg"); using (Image smallImage = new Bitmap(img1, new Size(img1.Width / 2, img1.Height / 2))) { //... }
6、獲取縮略圖
Bitmap myBitmap = new Bitmap("Climber.jpg"); Image myThumbnail = myBitmap.GetThumbnailImage(40, 40, null, IntPtr.Zero); e.Graphics.DrawImage(myThumbnail, 150, 75);
7、旋轉
Image img = Bitmap.FromFile(@"C:\music.bmp"); PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; PictureBox1.Image = img; //旋轉 img.RotateFlip(RotateFlipType.Rotate180FlipY); PictureBox1.Image = img;
8、雙倍緩沖
//創(chuàng)建一個與窗體工作區(qū)大小相同的空位圖 using (Bitmap image = new Bitmap(ClientRectangle.Width, ClientRectangle.Height))//創(chuàng)建位圖實例 { Graphics g = Graphics.FromImage(image);//以此圖像做畫布,畫圖形 g.FillRectangle(Brushes.White, ClientRectangle); g.DrawImage(image, ClientRectangle); //在窗體中繪制出內存中的圖像 }
9、格式轉換與保存:
img.Save("c:/1.jpg", ImageFormat.Jpeg); img.Save("c:/1.gif", ImageFormat.Gif);
二、打印 System.Drawing.Printing
1、打印預覽
PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler(pd_PrintPage); PrintPreviewDialog ppd = new PrintPreviewDialog(); ppd.Document = pd; ppd.ShowDialog();
2、打印
PrintDocument pd = new PrintDocument(); pd.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner GX430t"; //打印機名稱 pd.DefaultPageSettings.Landscape = true; //設置橫向打印,不設置默認是縱向的 pd.PrintPage += new PrintPageEventHandler(pd_PrintPage); pd.Print();
3、分頁打印文本文件
多頁打印必須把HasMorePages 設為true,達到需要的頁數(shù)后關掉此屬性。否則無窮添加新頁面!
當HasMorePages 設為true后,PrintDocument_PrintPage重復自我運行,直到HasMorePages 設為false。
private string text = string.Empty; private int top = 0; private Size textSize = Size.Empty; private void button1_Click(object sender, EventArgs e) { text = this.textBox1.Text; PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler(pd_PrintPage); pd.BeginPrint += new PrintEventHandler(pd_BeginPrint); using (System.Windows.Forms.PrintPreviewDialog dlg = new PrintPreviewDialog()) { dlg.Document = pd; dlg.WindowState = FormWindowState.Maximized; dlg.AllowTransparency = true; dlg.AutoScaleMode = AutoScaleMode.Dpi; dlg.ShowDialog(); } } private void pd_BeginPrint(object sender, PrintEventArgs e) { textSize = Size.Empty; top = 0; } private void pd_PrintPage(object sender, PrintPageEventArgs e) { if (textSize == Size.Empty) { textSize = Size.Round(e.Graphics.MeasureString(text, Font, e.MarginBounds.Width)); } e.Graphics.SetClip(e.MarginBounds); e.Graphics.DrawString(text, this.Font, Brushes.Black, new Rectangle(e.MarginBounds.Location.X, e.MarginBounds.Location.Y + top * -1, textSize.Width, textSize.Height)); ; if (top + e.MarginBounds.Height < textSize.Height) { top = top + e.MarginBounds.Height; e.HasMorePages = true; } }
到此這篇關于C#中圖像Image類與打印Printing類的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用DateTime的ParseExact方法實現(xiàn)特殊日期時間的方法詳解
本篇文章是對使用DateTime的ParseExact方法實現(xiàn)特殊日期時間的方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已安裝軟件變化的方法
這篇文章主要介紹了C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已安裝軟件變化的方法,涉及C#針對注冊表的讀取與監(jiān)控技巧,非常具有實用價值,需要的朋友可以參考下2015-08-08automation服務器不能創(chuàng)建對象 解決方法
本文主要介紹如何解決“automation服務器不能創(chuàng)建對象”錯誤,從而解決Visual Studio.Net不能正常使用的問題,需要的朋友可以參考下。2016-06-06