图像Image类、打印Printing相关
一、Images
1、概述
Image 类为Bitmap(位图) 和 Metafile(矢量图) 的类提供功能的抽象基类。Image类不能直接创建对象的,但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) 从指定的文件创建 Image。
- FromStream(Stream) 从指定的数据流创建 Image。
- GetBounds(GraphicsUnit) 以指定的单位获取图像的界限。
- GetThumbnailImage(Int32, Int32, Image+GetThumbnailImageAbort, IntPtr) 返回此 Image 的缩略图。
- RotateFlip(RotateFlipType) 旋转、翻转或者同时旋转和翻转 Image。
- Save(Stream, ImageFormat) 将此图像以指定的格式保存到指定的流中。
- Save(String, ImageFormat) 将此 Image 以指定格式保存到指定文件。
绘制图片:
- 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));
- }
缩放:
- Image img1 = new Bitmap(@"C:\1.jpg");
- using (Image smallImage = new Bitmap(img1, new Size(img1.Width / 2, img1.Height / 2)))
- {
- //...
- }
获取缩略图
- Bitmap myBitmap = new Bitmap("Climber.jpg");
- Image myThumbnail = myBitmap.GetThumbnailImage(40, 40, null, IntPtr.Zero);
- e.Graphics.DrawImage(myThumbnail, 150, 75);
旋转
- Image img = Bitmap.FromFile(@"C:\music.bmp");
- PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
- PictureBox1.Image = img;
- //旋转
- img.RotateFlip(RotateFlipType.Rotate180FlipY);
- PictureBox1.Image = img;
双倍缓冲
- //创建一个与窗体工作区大小相同的空位图
- using (Bitmap image = new Bitmap(ClientRectangle.Width, ClientRectangle.Height))//创建位图实例
- {
- Graphics g = Graphics.FromImage(image);//以此图像做画布,画图形
- g.FillRectangle(Brushes.White, ClientRectangle);
- g.DrawImage(image, ClientRectangle); //在窗体中绘制出内存中的图像
- }
格式转换与保存:
- 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,达到需要的页数后关掉此属性。否则无穷添加新页面!
当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;
- }
- }
图像Image类、打印Printing相关的更多相关文章
- Nao 类人机器人 相关资料
Nao 类人机器人 相关资料: 1.兄妹 PEPPER :在山东烟台生产,http://www.robot-china.com/news/201510/30/26564.html 2.国内机器人领先公 ...
- WorldWind源码剖析系列:图像助手类ImageHelper
图像助手类ImageHelper封装了对各种图像的操作.该类类图如下. 提供的主要处理方法基本上都是静态函数,简要描述如下: public static bool IsGdiSupportedImag ...
- 某音乐类App评论相关API的分析及SQL注入尝试
关键字:APIfen.工具使用.sql注入 涉及工具/包:Fiddler.Burpsuite.Js2Py.Closure Compiler.selenium.phantomjs.sqlmap 摘要: ...
- day15--Java常用类之日期相关类
Java常用类 3.日期相关类 3.1Date类 在标准Java类库中包含一个Date类,它的对象表示一个特定的瞬间,精确到毫秒.在网上商城下单时,在对报销单进行审核时,都需要获取当前的时间,通过Da ...
- 全文检索解决方案(lucene工具类以及sphinx相关资料)
介绍两种全文检索的技术. 1. lucene+ 中文分词(IK) 关于lucene的原理,在这里可以得到很好的学习. http://www.blogjava.net/zhyiwww/archive/ ...
- .net学习笔记----利用System.Drawing.Image类进行图片相关操作
C#中对图片的操作主要是通过System.Drawing.Image等类进行. 一.将图片转换为字节流 /// <summary> /// 图片处理帮助类 /// </summary ...
- App是什么,可以分为几类?及其相关解释。
App,是应用程序,Application的缩写,事实上,严格说来,目前市面上的APP大致可分为以下十类,即移动UGC,移动搜索,移动浏览,移动支付,移动广告,移动即时信息,SNS,LBS,AR以及 ...
- print打印网页相关
作者:zccst 1,CSS <link href="/style/print.css" rel="stylesheet" type="text ...
- PrintService类打印
系统打印服务框架代码位于android.printservice包中.系统并没有实现具体打印功能,需要打印机厂商制作插件接入系统打印服务之后,自行实现 主要类: PrintDocument:表示待打印 ...
随机推荐
- jira邮箱配置
系统-邮件-外发邮件
- [转帖]Linux系列之SAR命令使用详解
Linux系列之SAR命令使用详解 sar是System Activity Reporter(系统活动情况报告)的缩写.这个工具所需要的负载很小,也是目前linux中最为全面的性能分析工具之一.此款工 ...
- 【转帖】iPhone 11 Pro Max皇帝版物料成本不足3500元 卖一赚二
iPhone 11 Pro Max皇帝版物料成本不足3500元 卖一赚二 https://www.cnbeta.com/articles/tech/894449.htm 供应链的掌控力很重要 苹果今年 ...
- 推荐:【视频教程】ASP.NET Core 3.0 入门
墙裂推荐了,免费,通俗易懂,唯一可惜的就是不是我录的,更可惜的是人家录制完了快半年了我还没看完... 版权归原作者所有,建议新手还是边看边实践吧,要不然过完一遍发现自己啥也没学会,不要眼高手低 [视频 ...
- Servlet 响应及请求信息
// 文件路径 D:\ApacheServer\web_java\HelloWorld\src\com\test\TestServletRequestrResponse.java package co ...
- 【Docker】:使用docker安装mysql,挂载外部配置和数据
普通安装 1.下载镜像,mysql 5.7 docker pull mysql:5.7 2.创建mysql容器,并后台启动 docker run -d -p 3306:3306 -e MYSQL_US ...
- 1266: gcd和lcm(Java)
WUSTOJ 1266: gcd和lcm 参考 1naive1的博客 Description 已知a,b的最大公约数为x,也即gcd(a,b)=x; a,b的最小公倍数为y,也即lcm(a,b)= ...
- 原来你是这样的PaaS!
啥叫PaaS? 许多人身处互联网领域,对PaaS仍然是雾里看花.它看似复杂,其实只要用对看法,人人都可以轻松的认识它. 网络上盛传着用pizza为例子帮助人们了解什么是PaaS,那么编者今天也不举栗子 ...
- linux上启动tomcat报错:Failed to read schema document 'http://www.springframework.org/schema/data/mongo/spring-mongo-2.0.xsd
本文原文连接: http://blog.csdn.net/bluishglc/article/details/7596118 ,转载请注明出处! spring在加载xsd文件时总是先试图在本地查找xs ...
- css line-height & 图片底部间隙的处理
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 看大牛张鑫旭的视屏可能会理解的更深一些,点击这里 . line-height 的学习 line-heigh ...