C#中的圖像Image類與打印Printing類用法
一、Images
1、概述
Image 類為Bitmap(位圖) 和 Metafile(矢量圖) 的類提供功能的抽象基類。Image類不能直接創(chuàng)建對象的,但I(xiàn)mage.FromFile()返回的是Bitmap或者M(jìn)etafile的對象。
初始化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、分頁打印文本文件
多頁打印必須把HasMorePages 設(shè)為true,達(dá)到需要的頁數(shù)后關(guān)掉此屬性。否則無窮添加新頁面!
當(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類的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能
這篇文章主要介紹了C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能,需要的朋友可以參考下2017-12-12
WinForm中comboBox控件數(shù)據(jù)綁定實(shí)現(xiàn)方法
這篇文章主要介紹了WinForm中comboBox控件數(shù)據(jù)綁定實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了WinForm實(shí)現(xiàn)comboBox控件數(shù)據(jù)綁定的常用方法與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
C# byte轉(zhuǎn)為有符號(hào)整數(shù)實(shí)例
這篇文章主要介紹了C# byte轉(zhuǎn)為有符號(hào)整數(shù)實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
C#中使用快速排序按文件創(chuàng)建時(shí)間將文件排序的源碼
C#中使用快速排序按文件創(chuàng)建時(shí)間將文件排序的源碼...2007-03-03
DirectoryInfo引用一個(gè)相對目錄的實(shí)例
這種特殊參數(shù)在Windows的命令提示符或者“運(yùn)行”對話框中都可以使用,等價(jià)于DOS中的cd命令參數(shù)。直接上代碼,一看你就懂了:2013-04-04
C#中判斷、驗(yàn)證字符串是否為日期格式的實(shí)現(xiàn)代碼
這篇文章主要介紹了C#中判斷、驗(yàn)證字符串是否為日期格式的實(shí)現(xiàn)代碼,使用DateTime類中自帶的兩個(gè)方法實(shí)現(xiàn),需要的朋友可以參考下2014-08-08
C#滑動(dòng)驗(yàn)證碼拼圖驗(yàn)證功能實(shí)現(xiàn)(SlideCaptcha)
目前網(wǎng)站上的驗(yàn)證碼機(jī)制可謂是五花八門,有簡單的數(shù)字驗(yàn)證,有摻雜了字母和文字的混淆驗(yàn)證,還有通過滑塊進(jìn)行的拼圖驗(yàn)證,下面這篇文章主要給大家介紹了關(guān)于C#滑動(dòng)驗(yàn)證碼拼圖驗(yàn)證功能的實(shí)現(xiàn)方法,需要的朋友可以參考下2022-04-04

