C#中的图像Image类与打印Printing类用法
作者:springsnow 发布时间:2022-07-25 06:24:36
一、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 以指定格式保存到指定文件。
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、双倍缓冲
//创建一个与窗体工作区大小相同的空位图
using (Bitmap image = new Bitmap(ClientRectangle.Width, ClientRectangle.Height))//创建位图实例
{
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,达到需要的页数后关掉此属性。否则无穷添加新页面!
当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;
}
}
来源:https://www.cnblogs.com/springsnow/p/9433976.html
猜你喜欢
- spring in action第三版读书笔记spring3.0引入了spring expression language(spel)语言,
- 本文实例为大家分享了java实现银行ATM管理系统的具体代码,供大家参考,具体内容如下功能账户类、首页设计分析① 每个用户一个账户对象,需要
- 按官方修改的示例:#MidServerClient.javaimport feign.Param;import org.springfram
- 一、使用ThreadLocal实现当前登录信息的存取在项目中我们增加一个员工有一些信息是需要我们自己填入的,有一些信息不需要我们自己填写,例
- 如果你还不是很了解restful,或者认为restful只是一种规范不具有实际意义,推荐一篇osc两年前的文章:RESTful API 设计
- 今天无意中发现一个圆形进度,想想自己实现一个,如下图:基本思路是这样的:1.首先绘制一个实心圆2.绘制一个白色实心的正方形,遮住实心圆3.在
- C# Class写入Json/// <summary> /// 写入jso
- 在java程序开发中,ftp用的比较多,经常打交道,比如说向FTP服务器上传文件、下载文件,本文给大家介绍如何利用jakarta commo
- 先来看一个很简单的核心图片缩放方法:public static Bitmap scale(Bitmap bitmap, float scal
- MyBatis注解实现动态SQL在 Mybatis 中,使用注解可以很方便的进行sql操作,但很多动态 SQL 都是由 xml 配置实现的。
- 一,二分法检索算法介绍二分法检索(binary search)又称折半检索,二分法检索的基本思想是设字典中的元素从小到大有序地存放在数组(a
- using System;namespace Partial{ class Program { &nb
- 表单代码<!DOCTYPE html><html lang="en" xmlns="http
- ❤️大家好,我是贾斯汀,今天主要聊一聊关于线程的瓜!❤️先来看一下线程这张图线程的几种运行状态之间运行流程:看不懂没关系,慢慢来学习,往
- 本文实例讲述了Java使用桥接模式实现开关和电灯照明功能。分享给大家供大家参考,具体如下:一、模式定义桥接模式,也称桥梁模式,在软件系统中,
- 就是仿照现在扫一扫的形式,周围是半透明的遮挡,然后中间是全透明的,拍摄后只截取框内的内容查了很多博客,实现起来真的太复杂了,本人比较怕麻烦所
- Android:Field can be converted to a local varible.的解决办法前言:使用 Android S
- Spring2.5.6开发环境搭建的过程,供大家参考,具体内容如下1、jar 包准备:spring 2.5.6 的 jar 包(链接: ht
- 1. 基本概念的理解`绝对路径`:你应用上的文件或目录在硬盘上真正的路径,如:URL、物理路径例如:c:/xyz/test.txt代表了te
- 在eclipse中默认的maven,它加载的是国外的镜像,那样速度会比较慢,如果使用国内镜像,比如阿里的中央仓库;速度会快很多。那如何修改m