广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C#中的图像Image类与打印Printing类用法
  • 520
分享到

C#中的图像Image类与打印Printing类用法

2024-04-02 19:04:59 520人浏览 薄情痞子
摘要

一、Images 1、概述 Image 类为Bitmap(位图) 和 Metafile(矢量图) 的类提供功能的抽象基类。Image类不能直接创建对象的,但Image.FromFil

一、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;
    }
}

到此这篇关于C#中图像Image类与打印Printing类的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: C#中的图像Image类与打印Printing类用法

本文链接: https://www.lsjlt.com/news/148842.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C#中的图像Image类与打印Printing类用法
    一、Images 1、概述 Image 类为Bitmap(位图) 和 Metafile(矢量图) 的类提供功能的抽象基类。Image类不能直接创建对象的,但Image.FromFil...
    99+
    2022-11-13
  • C# 使用PrintDocument类打印标签的方法
    最近做了一个项目,使用不干胶标签贴在RFID抗金属标签上,那么就会出现标签打印的问题,该如何打印呢?后来经过网上冲浪发现,其实打印标签和打印A4纸的方法一样,只不过就是布局、设置纸张...
    99+
    2022-11-13
  • C++中图片类型的识别与转换详解方法
    目录1、图片类型的识别1.1、bmp图片1.2、jpg图片1.3、jpg图片1.4、gif图片1.5、tiff图片1.6、使用CreateFile和ReadFile API函数读取内...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作