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

在form1里有个宽度100,高度100,的pictureBox1.实际原图的PNG格式图片,是1000,1000.在form1只要吧pictureBox1设置为zoom... 在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);
展开
 我来答
老师你的烟掉了
推荐于2016-11-04 · 超过17用户采纳过TA的回答
知道答主
回答量:43
采纳率:100%
帮助的人:30.7万
展开全部
// <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();
}
}
1804438115
高粉答主

推荐于2016-04-01 · 醉心答题,欢迎关注
知道大有可为答主
回答量:1.1万
采纳率:61%
帮助的人:3765万
展开全部
对图片进行重绘,具体代码如下:
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);
}
}

以上代码可根据自己的情况进行修改,原理是一样的。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
清怨孤战
2014-09-30 · TA获得超过105个赞
知道小有建树答主
回答量:134
采纳率:100%
帮助的人:134万
展开全部
别直接打印原图,将原图缩放保存成新图像。最后打印新图像
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式