c# 打印里图片这么让png的高清大图按比例缩小,

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# 打印里图片这么让png的高清大图按比例缩小,相关的知识,希望对你有一定的参考价值。

在form1里有个宽度100,高度100,的pictureBox1.
实际原图的PNG格式图片,是1000,1000.
在form1只要吧pictureBox1设置为zoom就按比例缩小了。
但是在打印里显示的就是只有一个角了

Rectangle destRect = new Rectangle(857, 490 + 45, pictureBox3.Width, pictureBox3.Height);
e.Graphics.DrawImage(pictureBox3.Image, destRect, 0,0, pictureBox3.Width, pictureBox3.Height, GraphicsUnit.Pixel);Graphics.DrawImage(pictureBox3.Image, destRect, 0,0, pictureBox3.Width, pictureBox3.Height, GraphicsUnit.Pixel);

// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)

System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;

int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;

switch (mode)

case "HW": //指定高宽缩放(可能变形)
break;
case "W": //指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H": //指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut": //指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)

oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;

else

ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;

break;
default:
break;


//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);

//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);

try

//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);

catch (System.Exception e)

throw e;

finally

originalImage.Dispose();
bitmap.Dispose();
g.Dispose();

参考技术A 对图片进行重绘,具体代码如下:
public class CImageLibrary

public enum ValidateImageResult OK, InvalidFileSize, InvalidImageSize

//检查图片大小
public static ValidateImageResult ValidateImage(string file, int MAX_FILE_SIZE, int MAX_WIDTH, intMAX_HEIGHT)

byte[] bs = File.ReadAllBytes(file);

double size = (bs.Length / 1024);
//大于50KB
if (size > MAX_FILE_SIZE) return ValidateImageResult.InvalidFileSize;
Image img = Image.FromFile(file);
if (img.Width > MAX_WIDTH || img.Height > MAX_HEIGHT) returnValidateImageResult.InvalidImageSize;
return ValidateImageResult.OK;


//按宽度比例缩小图片
public static Image GetOutputSizeImage(Image imgSource, int MAX_WIDTH)

Image imgOutput = imgSource;

Size size = new Size(imgSource.Width, imgSource.Height);
if (imgSource.Width <= 3 || imgSource.Height <= 3) return imgSource; //3X3大小的图片不转换

if (imgSource.Width > MAX_WIDTH || imgSource.Height > MAX_WIDTH)

double rate = MAX_WIDTH / (double)imgSource.Width;

if (imgSource.Height * rate > MAX_WIDTH)
rate = MAX_WIDTH / (double)imgSource.Height;

size.Width = Convert.ToInt32(imgSource.Width * rate);
size.Height = Convert.ToInt32(imgSource.Height * rate);

imgOutput = imgSource.GetThumbnailImage(size.Width, size.Height, null, IntPtr.Zero);


return imgOutput;


//按比例缩小图片
public static Image GetOutputSizeImage(Image imgSource, Size outSize)

Image imgOutput = imgSource.GetThumbnailImage(outSize.Width, outSize.Height, null, IntPtr.Zero);
return imgOutput;


public static byte[] GetImageBytes(string imageFileName)

Image img = Image.FromFile(imageFileName);
return GetImageBytes(img);


public static byte[] GetImageBytes(Image img)

if (img == null) return null;
try

System.IO.MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Jpeg);
byte[] bs = ms.ToArray();
ms.Close();
return bs;

catch return null;


public static Image FromBytes(byte[] bs)

if (bs == null) return null;
try

MemoryStream ms = new MemoryStream(bs);
Image returnImage = Image.FromStream(ms);
ms.Close();
return returnImage;


catch return null;



上传文件自定义类. 业务逻辑全部在CFileUpload类!

public class CFileUpload

private FileUpload _fileUpload;
private string _savePath;
private string _LastUploadedFile = string.Empty;
private bool _AutoGenFileName = false;
public string LastUploadedFile get return _LastUploadedFile;

private string PICTURE_FILE = "[.gif.png.jpeg.jpg.bmp]";
private string ZIP_FILE = "[.zip.rar]";
private string MUILT_MEDIA_FILE = "[.mpeg.mpg.fla.exe.wma]";

private int IMG_MAX_WIDTH = 0;//未指定宽度
private int IMG_MAX_HEIGHT = 0;//未指定高度

/// <summary>
/// 构造器
/// </summary>
/// <param name="fileUpload">Asp.net FileUpload对象</param>
/// <param name="savePath">保存目录,不包含文件名</param>
/// <param name="autoGenFileName">自动生成文件名</param>
public CFileUpload(FileUpload fileUpload, string savePath, bool autoGenFileName)

_savePath = savePath;
_fileUpload = fileUpload;
_AutoGenFileName = autoGenFileName;


/// <summary>
/// 构造器
/// </summary>
/// <param name="fileUpload">Asp.net FileUpload对象</param>
/// <param name="savePath">保存目录,不包含文件名</param>
public CFileUpload(FileUpload fileUpload, string savePath)

_savePath = savePath;
_fileUpload = fileUpload;


/// <summary>
/// 上传RAR文件
/// </summary>
public bool UploadRARFile()

return DoUpload(ZIP_FILE);


/// <summary>
/// 上传视频文件
/// </summary>
public bool UploadVideo()

return DoUpload(MUILT_MEDIA_FILE);


/// <summary>
/// 上传图片文件
/// </summary>
public bool UploadImage()

return DoUpload(PICTURE_FILE);


public bool UploadImage(int maxWidth, int maxHeight)

this.IMG_MAX_WIDTH = maxWidth;
this.IMG_MAX_HEIGHT = maxHeight;
return DoUpload(PICTURE_FILE);


/// <summary>
/// 上传任何支持的文件
/// </summary>
public bool UploadAnySupported()

return DoUpload(PICTURE_FILE + ZIP_FILE + MUILT_MEDIA_FILE);


/// <summary>
/// 生成新的文件名
/// </summary>
private string GetNewFileName(string folder, string fileName)

//_AutoGenFileName==true 或者文件名长度>50,自动生成32位GUID文件名
if (_AutoGenFileName || StrUtils.GetStringLength(fileName) >= 50)

string ext = System.IO.Path.GetExtension(fileName);
string newfile = Guid.NewGuid().ToString().Replace("-", "") + ext;
return folder + newfile;

else

if (System.IO.File.Exists(folder + fileName))

string ext = System.IO.Path.GetExtension(fileName);
string filebody = fileName.Replace(ext, "");

int x = 1;
while (true) //如果文件存在,生成尾部带(x)的文件

string newfile = folder + filebody + "(" + x.ToString() + ")" + ext;
if (!System.IO.File.Exists(newfile))
return folder + filebody + "(" + x.ToString() + ")" + ext;
else
x++;


else
return folder + fileName;



/// <summary>
/// 最大支持小于1MB的文件。
/// </summary>
private bool AllowMaxSize(int fileLength)

int MAX_SIZE_UPLOAD = 1024;//最大支持上传小于1MB的文件。
double kb = fileLength / 1024;
return (int)kb < MAX_SIZE_UPLOAD;


private bool DoUpload(string allowedExtensions)

bool fileOK = false;

if (!_fileUpload.HasFile) return false; //上传控件中如果不包含文件,退出

// 得到文件的后缀
string fileExtension = System.IO.Path.GetExtension(_fileUpload.FileName).ToLower();

// 看包含的文件是否是被允许的文件后缀
fileOK = allowedExtensions.IndexOf(fileExtension) > 0;

//检查上传文件大小
fileOK = fileOK & AllowMaxSize(_fileUpload.FileBytes.Length);

if (!fileOK) return false; //如检查不通过,退出

try

// 文件另存在服务器指定目录下
string savefile = GetNewFileName(_savePath, _fileUpload.FileName);

if (IsUploadImage(fileExtension))//保存图片

System.Drawing.Image output = CImageLibrary.FromBytes(_fileUpload.FileBytes);

// 检查图片宽度/高度/大小
if (this.IMG_MAX_WIDTH != 0 && output.Width > this.IMG_MAX_WIDTH)

output = CImageLibrary.GetOutputSizeImage(output, this.IMG_MAX_WIDTH);

Bitmap bmp = new Bitmap(output);
bmp.Save(savefile, output.RawFormat);

else//其它任何文件

_fileUpload.PostedFile.SaveAs(savefile);


_LastUploadedFile = savefile;
return true;

catch

return false;



private bool IsUploadImage(string fileExtension)

bool isImage = PICTURE_FILE.IndexOf(fileExtension) > 0;
return isImage;



使用说明:

页面的Button.Click事件

protected void btnUploadImg_Click(object sender, EventArgs e)

try

if (!FileUploadImg.HasFile) return;

string virtualFilePath = "~//" + CAppConfiguration.Current.BbsUploadFilePath;
string savePath = this.Server.MapPath(virtualFilePath); //保存文件物理路径

//调用CFileUpload类
CFileUpload upload = new CFileUpload(FileUploadImg, savePath, false);

//上传图片文件
bool ret = upload.UploadImage();
if (ret)

string filename = System.IO.Path.GetFileName(upload.LastUploadedFile);
string imghtml = "<img alt=''''贴图图片'''' src=''''0'''' />";
this.hfLastUploadImage.Value = filename;

string root = CGlobals.GetHttpRoot(this);

imgHTML = string.Format(imgHTML, root + "/" + CAppConfiguration.Current.BbsUploadFilePath + filename);
FreeTextBox1.Text = FreeTextBox1.Text + "<br/>" + imgHTML;

lblUploadImg.ForeColor = Color.Green;
lblUploadImg.Visible = true;
lblUploadImg.Text = "上传文件成功!";

else

lblUploadImg.ForeColor = Color.Red;
lblUploadImg.Visible = true;
lblUploadImg.Text = "不支持格式或上传失败!";


catch (Exception ex)

CMsg.ShowException(this, ex);



以上代码可根据自己的情况进行修改,原理是一样的。
参考技术B 别直接打印原图,将原图缩放保存成新图像。最后打印新图像

用Ps按比例缩小图片整体的尺寸

有时候前端拿到的PSD源文件很大,可能有几百MB甚至上G,可以按整体比例缩小尺寸

 

1,点击顶部“图像”,选择“图像大小”;

2,勾选“约束比例”,修改长或者宽的像素,另外一个像素即可随之缩放;

3,保存即可。

 

 

以上是关于c# 打印里图片这么让png的高清大图按比例缩小,的主要内容,如果未能解决你的问题,请参考以下文章

WPF 如何实现图片原比例缩小

android中ImageView放大和缩小相关问题?

CSDN博客一键调整图片大小按比例缩放居中最佳方法

用CSS缩小超过规定长度的图片

C# 图片缩放打印

fastreport4 picture 动态 设定大小,我的产品图像打印需要从数据库中读取,再按一定比例进行缩小。