C# 图片缩放打印

比如打印各种证书,将想要的图片加载到PICTUREBOX,在上面拖动LABEL,然后打印。问题是PICTUREBOX的大小和图片的大小不一致,打印出来的效果就是label... 比如打印各种证书,将想要的图片加载到PICTUREBOX,在上面拖动LABEL,然后打印。问题是PICTUREBOX的大小和图片的大小不一致,打印出来的效果就是label的错位,希望有高人指点! 展开
 我来答
wdby
2010-05-11 · TA获得超过1792个赞
知道小有建树答主
回答量:590
采纳率:0%
帮助的人:661万
展开全部
图像缩放成指定大小:

public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidth)
{
System.Drawing.Image imgSource = b;
System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;
int sW = 0, sH = 0;
// 按比例缩放
int sWidth = imgSource.Width;
int sHeight = imgSource.Height;

if (sHeight > destHeight || sWidth > destWidth)
{
if ((sWidth * destHeight) > (sHeight * destWidth))
{
sW = destWidth;
sH = (destWidth * sHeight) / sWidth;
}
else
{
sH = destHeight;
sW = (sWidth * destHeight) / sHeight;
}
}
else
{
sW = sWidth;
sH = sHeight;
}

Bitmap outBmp = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage(outBmp);
g.Clear(Color.Transparent);

// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
g.Dispose();

// 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;

EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;

imgSource.Dispose();

return outBmp;
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
莫凉辛
2010-05-11 · TA获得超过103个赞
知道小有建树答主
回答量:153
采纳率:0%
帮助的人:76.3万
展开全部
PICTUREBOX有个图片缩放属性,叫SizeMode,设置为StretchImage.就等于固定了图片大小
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
辟密钮悦
2019-08-24 · TA获得超过3903个赞
知道大有可为答主
回答量:3127
采纳率:28%
帮助的人:215万
展开全部
图像缩放成指定大小:
public
static
Bitmap
GetThumbnail(Bitmap
b,
int
destHeight,
int
destWidth)
{
System.Drawing.Image
imgSource
=
b;
System.Drawing.Imaging.ImageFormat
thisFormat
=
imgSource.RawFormat;
int
sW
=
0,
sH
=
0;
//
按比例缩放
int
sWidth
=
imgSource.Width;
int
sHeight
=
imgSource.Height;
if
(sHeight
>
destHeight
||
sWidth
>
destWidth)
{
if
((sWidth
*
destHeight)
>
(sHeight
*
destWidth))
{
sW
=
destWidth;
sH
=
(destWidth
*
sHeight)
/
sWidth;
}
else
{
sH
=
destHeight;
sW
=
(sWidth
*
destHeight)
/
sHeight;
}
}
else
{
sW
=
sWidth;
sH
=
sHeight;
}
Bitmap
outBmp
=
new
Bitmap(destWidth,
destHeight);
Graphics
g
=
Graphics.FromImage(outBmp);
g.Clear(Color.Transparent);
//
设置画布的描绘质量
g.CompositingQuality
=
CompositingQuality.HighQuality;
g.SmoothingMode
=
SmoothingMode.HighQuality;
g.InterpolationMode
=
InterpolationMode.HighQualityBicubic;
g.DrawImage(imgSource,
new
Rectangle((destWidth
-
sW)
/
2,
(destHeight
-
sH)
/
2,
sW,
sH),
0,
0,
imgSource.Width,
imgSource.Height,
GraphicsUnit.Pixel);
g.Dispose();
//
以下代码为保存图片时,设置压缩质量
EncoderParameters
encoderParams
=
new
EncoderParameters();
long[]
quality
=
new
long[1];
quality[0]
=
100;
EncoderParameter
encoderParam
=
new
EncoderParameter(System.Drawing.Imaging.Encoder.Quality,
quality);
encoderParams.Param[0]
=
encoderParam;
imgSource.Dispose();
return
outBmp;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式