C# 图片缩放打印
比如打印各种证书,将想要的图片加载到PICTUREBOX,在上面拖动LABEL,然后打印。问题是PICTUREBOX的大小和图片的大小不一致,打印出来的效果就是label...
比如打印各种证书,将想要的图片加载到PICTUREBOX,在上面拖动LABEL,然后打印。问题是PICTUREBOX的大小和图片的大小不一致,打印出来的效果就是label的错位,希望有高人指点!
展开
3个回答
展开全部
图像缩放成指定大小:
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;
}
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;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
PICTUREBOX有个图片缩放属性,叫SizeMode,设置为StretchImage.就等于固定了图片大小
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
图像缩放成指定大小:
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;
}
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询