C#中的圖像Image類與打印Printing類用法
一、Images
1、概述
Image 類為Bitmap(位圖) 和 Metafile(矢量圖) 的類提供功能的抽象基類。Image類不能直接創(chuàng)建對(duì)象的,但I(xiàn)mage.FromFile()返回的是Bitmap或者M(jìn)etafile的對(duì)象。
初始化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):旋轉(zhuǎn)、翻轉(zhuǎn)或者同時(shí)旋轉(zhuǎn)和翻轉(zhuǎn) 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、旋轉(zhuǎn)
Image img = Bitmap.FromFile(@"C:\music.bmp"); PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; PictureBox1.Image = img; //旋轉(zhuǎn) img.RotateFlip(RotateFlipType.Rotate180FlipY); PictureBox1.Image = img;
8、雙倍緩沖
//創(chuàng)建一個(gè)與窗體工作區(qū)大小相同的空位圖 using (Bitmap image = new Bitmap(ClientRectangle.Width, ClientRectangle.Height))//創(chuàng)建位圖實(shí)例 { Graphics g = Graphics.FromImage(image);//以此圖像做畫布,畫圖形 g.FillRectangle(Brushes.White, ClientRectangle); g.DrawImage(image, ClientRectangle); //在窗體中繪制出內(nèi)存中的圖像 }
9、格式轉(zhuǎn)換與保存:
img.Save("c:/1.jpg", ImageFormat.Jpeg); img.Save("c:/1.gif", ImageFormat.Gif);
二、打印 System.Drawing.Printing
1、打印預(yù)覽
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"; //打印機(jī)名稱 pd.DefaultPageSettings.Landscape = true; //設(shè)置橫向打印,不設(shè)置默認(rèn)是縱向的 pd.PrintPage += new PrintPageEventHandler(pd_PrintPage); pd.Print();
3、分頁(yè)打印文本文件
多頁(yè)打印必須把HasMorePages 設(shè)為true,達(dá)到需要的頁(yè)數(shù)后關(guān)掉此屬性。否則無(wú)窮添加新頁(yè)面!
當(dāng)HasMorePages 設(shè)為true后,PrintDocument_PrintPage重復(fù)自我運(yùn)行,直到HasMorePages 設(shè)為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; } }
到此這篇關(guān)于C#中圖像Image類與打印Printing類的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用DateTime的ParseExact方法實(shí)現(xiàn)特殊日期時(shí)間的方法詳解
本篇文章是對(duì)使用DateTime的ParseExact方法實(shí)現(xiàn)特殊日期時(shí)間的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
這篇文章主要介紹了C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法,涉及C#針對(duì)注冊(cè)表的讀取與監(jiān)控技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-08-08C#獲取文件、文件夾和驅(qū)動(dòng)器的信息示例詳解
在C#中,文件、文件夾和驅(qū)動(dòng)器是文件系統(tǒng)操作的基本元素,了解如何獲取這些元素的信息對(duì)于開(kāi)發(fā)文件處理和管理工具至關(guān)重要,本文將詳細(xì)介紹如何在C#中獲取有關(guān)文件、文件夾和驅(qū)動(dòng)器的信息,并提供相應(yīng)的示例,需要的朋友可以參考下2024-08-08使用C#實(shí)現(xiàn)網(wǎng)頁(yè)內(nèi)容保存為圖片并生成壓縮包
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)網(wǎng)頁(yè)內(nèi)容保存為圖片并生成壓縮包,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02automation服務(wù)器不能創(chuàng)建對(duì)象 解決方法
本文主要介紹如何解決“automation服務(wù)器不能創(chuàng)建對(duì)象”錯(cuò)誤,從而解決Visual Studio.Net不能正常使用的問(wèn)題,需要的朋友可以參考下。2016-06-06