当前位置: 首页 > 编程语言 > C#

C#中Image类和打印类用法分享

时间:2023-04-10 16:18:47 C#

一、Images1.概述Image类是一个抽象基类,为Bitmap(位图)和Metafile(矢量图)类提供功能。Image类不能直接创建对象,但Image.FromFile()返回Bitmap或Metafile对象。初始化图片:Imageimg0=Image.FromFile(@"C:1.jpg");图片img1=Image.FromStream(File.OpenRead(@"C:1.jpg"));Imageimg2=newBitmap(@"C:1.jpg");2.属性PixelFormat:获取此Image的像素格式。RawFormat:获取此Image的文件格式。Size:获取此图像的宽度和高度(以像素为单位)。宽度:获取此图像的宽度(以像素为单位)。高度:获取此图像的高度(以像素为单位)。3.方法FromFile(String):从指定文件创建图像。FromStream(Stream):从指定的流创建一个图像。GetBounds(GraphicsUnit):以指定单位获取图像的边界。GetThumbnailImage(Int32,Int32,Image+GetThumbnailImageAbort,IntPtr):返回此图像的缩略图。RotateFlip(RotateFlipType):旋转、翻转或同时旋转和翻转图像。Save(Stream,ImageFormat):将此图像以指定格式保存到指定流。Save(String,ImageFormat):将这个Image以指定的格式保存到指定的文件中。4、绘制图片:using(Imageimg=newBitmap(@"C:1.jpg")){System.Drawing.Graphicsg=Graphics.FromImage(img);g.DrawImage(img,newSystem.Drawing.Rectangle(0,0,img.Width,img.Height));5.缩放:Imageimg1=newBitmap(@"C:1.jpg");using(ImagesmallImage=newBitmap(img1,newSize(img1.Width/2,img1.Height/2))){//...}6.获取缩略图位图myBitmap=newBitmap("Climber.jpg");图片myThumbnail=myBitmap.GetThumbnailImage(40,40,null,IntPtr.Zero);e.Graphics.DrawImage(myThumbnail,150,75);7.旋转图像img=Bitmap.FromFile(@"C:music.bmp");PictureBox1.SizeMode=PictureBoxSizeMode.AutoSize;PictureBox1.Image=img;//旋转img.RotateFlip(RotateFlipType.Rotate180FlipY);PictureBox1.Image=img;8.双缓冲//创建一个与窗体工作区大小相同的空位图using(Bitmapimage=newBitmap(ClientRectangle.Width,ClientRectangle.Height))//创建一个位图实例{Graphicsg=Graphics.FromImage(image);//用这张图片作为画布绘制图形g.FillRectangle(Brushes.White,ClientRectangle);g.DrawImage(我法师,客户端矩形);//在窗体上绘制内存中的图像}9.格式转换和保存:img.Save("c:/1.jpg",ImageFormat.Jpeg);img.Save("c:/1.gif",ImageFormat.Gif);二、打印System.Drawing.Printing1、打印预览PrintDocumentpd=newPrintDocument();pd.PrintPage+=newPrintPageEventHandler(pd_PrintPage);PrintPreviewDialogppd=newPrintPreviewDialog();ppd.Document=pd;ppd.ShowDialog();2.打印PrintDocumentpd=newPrintDocument();pd.DefaultPageSettings.PrinterSettings.PrinterName="ZDesignerGX430t";//打印机名称pd.DefaultPageSettings.Landscape=true;//设置水平打印,不设置则默认垂直pd.PrintPage+=newPrintPageEventHandler(pd_PrintPage);pd.打印();3、对于文本文件的多页打印,必须将HasMorePages设置为true,达到需要的页数后关闭该属性或者无限添加新页!当HasMorePages设置为true时,PrintDocument_PrintPage会重复自身,直到HasMorePages设置为false。私有字符串文本=string.Empty;私人诠释顶=0;私人大小textSize=Size.Empty;privatevoidbutton1_Click(objectsender,EventArgse){text=this.textBox1.Text;PrintDocumentpd=newPrintDocument();pd.PrintPage+=newPrintPageEventHandler(pd_PrintPage);pd.BeginPrint+=newPrintEventHandler(pd_BeginPrint);使用(System.Windows.Forms.PrintPreviewDialogdlg=newPrintPreviewDialog()){dlg.Document=pd;dlg.WindowState=FormWindowState.Maximized;dlg.AllowTransparency=true;dlg.AutoScaleMode=AutoScaleMode.Dpi;dlg.ShowDialog();}}privatevoidpd_BeginPrint(objectsender,PrintEventArgse){textSize=Size.Empty;顶部=0;}privatevoidpd_PrintPage(objectsender,PrintPageEventArgse){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,newRectangle(e.MarginBounds.Location.X,e.MarginBounds.Location.Y+top*-1,textSize.Width,textSize.Height));;if(top+e.MarginBounds.Height