c#窗体程序图片填充求助!!! 小白只会用texturebrush平铺填充效果就成了p2想要的效果是图片居中填满格子,而且只要一张图,不平铺!!!求助,50分是小弟的全部家产了orz 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary>/// 填充模式/// </summary>/// <res></res>public enum FillMode{ /// <summary> /// 平铺 /// </summary> /// <res></res> Title = 0, /// <summary> /// 居中 /// </summary> /// <res></res> Center = 1, /// <summary> /// 拉伸 /// </summary> /// <res></res> Struk = 2, /// <summary> /// 缩放 /// </summary> /// <res></res> Zoom = 3}/// <summary>/// 将指向图像按指定的填充模式绘制到目标图像上/// </summary>/// <param name="SourceBmp">要控制填充模式的源图</param>/// <param name="TargetBmp">要绘制到的目标图</param>/// <param name="_FillMode">填充模式</param>/// <res></res>public void Image_FillRect(Bitmap SourceBmp, Bitmap TargetBmp, FillMode _FillMode){ try { switch (_FillMode) { case FillMode.Title: using (TextureBrush Txbrus = new TextureBrush(SourceBmp)) { Txbrus.WrapMode = Drawing2D.WrapMode.Tile; using (Graphics G = Graphics.FromImage(TargetBmp)) { G.FillRectangle(Txbrus, new Rectangle(0, 0, TargetBmp.Width - 1, TargetBmp.Height - 1)); } } break; case FillMode.Center: using (Graphics G = Graphics.FromImage(TargetBmp)) { int xx = (TargetBmp.Width - SourceBmp.Width) / 2; int yy = (TargetBmp.Height - SourceBmp.Height) / 2; G.DrawImage(SourceBmp, new Rectangle(xx, yy, SourceBmp.Width, SourceBmp.Height), new Rectangle(0, 0, SourceBmp.Width, SourceBmp.Height), GraphicsUnit.Pixel); } break; case FillMode.Struk: using (Graphics G = Graphics.FromImage(TargetBmp)) { G.DrawImage(SourceBmp, new Rectangle(0, 0, TargetBmp.Width, TargetBmp.Height), new Rectangle(0, 0, SourceBmp.Width, SourceBmp.Height), GraphicsUnit.Pixel); } break; case FillMode.Zoom: double tm = 0.0; int W = SourceBmp.Width; int H = SourceBmp.Height; if (W > TargetBmp.Width) { tm = TargetBmp.Width / SourceBmp.Width; W = W * tm; H = H * tm; } if (H > TargetBmp.Height) { tm = TargetBmp.Height / H; W = W * tm; H = H * tm; } using (Bitmap tmpBP = new Bitmap(W, H)) { using (Graphics G2 = Graphics.FromImage(tmpBP)) { G2.DrawImage(SourceBmp, new Rectangle(0, 0, W, H), new Rectangle(0, 0, SourceBmp.Width, SourceBmp.Height), GraphicsUnit.Pixel); using (Graphics G = Graphics.FromImage(TargetBmp)) { int xx = (TargetBmp.Width - W) / 2; int yy = (TargetBmp.Height - H) / 2; G.DrawImage(tmpBP, new Rectangle(xx, yy, W, H), new Rectangle(0, 0, W, H), GraphicsUnit.Pixel); } } } break; } } catch (Exception ex) { Console.WriteLine(ex.Message); }} 面试题N次出现,求解! 如何根据DropDownList值修改其他数据 webbrowser控件这种问题大家遇到过没有? C#如何操作Excel? 保存按钮(关于写入文件的问题) textbox的问题 Graphics.DrawString的问题,如何让他以字符为单位换行,而不是单词 请教空格替换 问题 大哥们.web安装项目时能不能同时注册组件? 如何记录本机一天所使用过的所有应用程序,以及所有浏览过的网站.(分不够可以加) 求C#读写dwg文件(不使用AntoCAD环境)的开源插件或代码! 大佬们,设计时的坐标输入后为什么运行时位置不同啊
/// 填充模式
/// </summary>
/// <res></res>
public enum FillMode
{
/// <summary>
/// 平铺
/// </summary>
/// <res></res>
Title = 0,
/// <summary>
/// 居中
/// </summary>
/// <res></res>
Center = 1,
/// <summary>
/// 拉伸
/// </summary>
/// <res></res>
Struk = 2,
/// <summary>
/// 缩放
/// </summary>
/// <res></res>
Zoom = 3
}
/// <summary>
/// 将指向图像按指定的填充模式绘制到目标图像上
/// </summary>
/// <param name="SourceBmp">要控制填充模式的源图</param>
/// <param name="TargetBmp">要绘制到的目标图</param>
/// <param name="_FillMode">填充模式</param>
/// <res></res>
public void Image_FillRect(Bitmap SourceBmp, Bitmap TargetBmp, FillMode _FillMode)
{
try {
switch (_FillMode) {
case FillMode.Title:
using (TextureBrush Txbrus = new TextureBrush(SourceBmp)) {
Txbrus.WrapMode = Drawing2D.WrapMode.Tile;
using (Graphics G = Graphics.FromImage(TargetBmp)) {
G.FillRectangle(Txbrus, new Rectangle(0, 0, TargetBmp.Width - 1, TargetBmp.Height - 1));
}
} break;
case FillMode.Center:
using (Graphics G = Graphics.FromImage(TargetBmp)) {
int xx = (TargetBmp.Width - SourceBmp.Width) / 2;
int yy = (TargetBmp.Height - SourceBmp.Height) / 2;
G.DrawImage(SourceBmp, new Rectangle(xx, yy, SourceBmp.Width, SourceBmp.Height), new Rectangle(0, 0, SourceBmp.Width, SourceBmp.Height), GraphicsUnit.Pixel);
} break;
case FillMode.Struk:
using (Graphics G = Graphics.FromImage(TargetBmp)) {
G.DrawImage(SourceBmp, new Rectangle(0, 0, TargetBmp.Width, TargetBmp.Height), new Rectangle(0, 0, SourceBmp.Width, SourceBmp.Height), GraphicsUnit.Pixel);
} break;
case FillMode.Zoom:
double tm = 0.0;
int W = SourceBmp.Width;
int H = SourceBmp.Height;
if (W > TargetBmp.Width) {
tm = TargetBmp.Width / SourceBmp.Width;
W = W * tm;
H = H * tm;
}
if (H > TargetBmp.Height) {
tm = TargetBmp.Height / H;
W = W * tm;
H = H * tm;
}
using (Bitmap tmpBP = new Bitmap(W, H)) {
using (Graphics G2 = Graphics.FromImage(tmpBP)) {
G2.DrawImage(SourceBmp, new Rectangle(0, 0, W, H), new Rectangle(0, 0, SourceBmp.Width, SourceBmp.Height), GraphicsUnit.Pixel);
using (Graphics G = Graphics.FromImage(TargetBmp)) {
int xx = (TargetBmp.Width - W) / 2;
int yy = (TargetBmp.Height - H) / 2;
G.DrawImage(tmpBP, new Rectangle(xx, yy, W, H), new Rectangle(0, 0, W, H), GraphicsUnit.Pixel);
}
}
} break;
}
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}