请问如何实现给图片加图片边框也就是相框 请问怎么用C#实现给图片加上边框也就是相框。就像:这种相框,把目标图片镶嵌在里面,谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 首先放个Panel,将边框图片作为背景backgroundimage然后里边放你的照片picturebox,拉伸到适当大小。再将PictureBox的Anchor设为Top, Bottom, Left, Right 先放个Panel, 背景色,设成你需要的边框色,再放一个图片入其中,将两个尺寸设成一样,图片的定位点设为0,0,即可 我在网上找了个代码 加图片水印的:/// <summary> /// WaterMark 的摘要说明 /// </summary> /// /// <param name="strCopyright">要加入的文字</param> /// <param name="strMarkPath">水印图片路径</param> /// <param name="strPhotoPath">要加水印的图片路径</param> /// <param name="strSavePath">处理后的图片路径</param> /// <param name="iMarkRightSpace">水印在修改图片中距左边的宽度</param> /// <param name="iMarkButtomSpace">水印在修改图片中距底部的高度</param> /// <param name="iDiaphaneity">水印图片的透明度</param> /// <param name="iFontRightSpace">文字</param> /// <param name="iFontButtomSpace">文字</param> /// <param name="iFontDiaphaneity">文字</param> /// <param name="bShowCopyright">是否显示文字</param> /// <param name="bShowMarkImage">是否显示水印图片</param> public class WaterMark { #region param private string strCopyright, strMarkPath, strPhotoPath, strSavePath; private int iMarkRightSpace, iMarkButtomSpace, iDiaphaneity; private int iFontRightSpace = 0, iFontButtomSpace = 0, iFontDiaphaneity = 80; private int iFontSize = 10; private bool bShowCopyright = true, bShowMarkImage = true; #endregion #region WaterMark public WaterMark() { this.strCopyright = ""; this.strMarkPath = null; this.strPhotoPath = null; this.strSavePath = null; this.iDiaphaneity = 50;//图片水印透明度 this.iMarkRightSpace = 0; this.iMarkButtomSpace = 0; } /// <summary> /// 主要用两样都加的 /// </summary> public WaterMark(string copyright, string Path, string photoPath, string savePath,int _iDiaphaneity) { this.strCopyright = copyright; this.strMarkPath = Path; this.strPhotoPath = photoPath; this.strSavePath = savePath; this.iDiaphaneity = _iDiaphaneity;//图片水印透明度 this.iMarkRightSpace = 0; this.iMarkButtomSpace = 0; } #endregion #region property /// <summary> /// 设置是否显示水印文字 /// </summary> public bool ShowCopyright { set { this.bShowCopyright = value; } } /// <summary> /// 设置是否显示水印图片 /// </summary> public bool ShowMarkImage { set { this.bShowMarkImage = value; } } /// <summary> /// 获取或设置要加入的文字 /// </summary> public string Copyright { set { this.strCopyright = value; } } /// <summary> /// 获取或设置加水印后的图片路径 /// </summary> public string SavePath { get { return this.strSavePath; } set { this.strSavePath = value; } } /// <summary> /// 获取或设置水印图片路径 /// </summary> public string MarkPath { get { return this.strMarkPath; } set { this.strMarkPath = value; } } /// <summary> /// 获取或设置要加水印图片的路径 /// </summary> public string PhotoPath { get { return this.strPhotoPath; } set { this.strPhotoPath = value; } } /// <summary> /// 设置水印图片的透明度 /// </summary> public int Diaphaneity { set { if (value > 0 && value <= 100) this.iDiaphaneity = value; } } /// <summary> /// 设置水印字体的透明度0-255 /// </summary> public int FontDiaphaneity { set { if (value >= 0 && value <= 255) this.iFontDiaphaneity = value; } } /// <summary> /// 设置水印图片在修改图片中距左边的高度 /// </summary> public int MarkRightSpace { set { this.iMarkRightSpace = value; } } /// <summary> /// 设置水印图片在修改图片中距底部的高度 /// </summary> public int MarkButtomSpace { set { this.iMarkButtomSpace = value; } } /// <summary> /// 设置水印字体在修改图片中距左边的距离 /// </summary> public int FontRightSpace { set { iFontRightSpace = value; } } /// <summary> /// 设置水印字体在修改图片中距底部的高度 /// </summary> public int FontButtomSpace { set { iFontButtomSpace = value; } } #endregion /// <summary> /// 生成水印图片 /// </summary> /// <returns></returns> public void createMarkPhoto() { Bitmap bmWater = null; Image gPhoto = Image.FromFile(this.strPhotoPath); int PhotoWidth = gPhoto.Width; int PhotoHeight = gPhoto.Height; Bitmap bitPhoto = new Bitmap(PhotoWidth, PhotoHeight, PixelFormat.Format24bppRgb); bitPhoto.SetResolution(gPhoto.HorizontalResolution, gPhoto.VerticalResolution); try { if (bShowCopyright) { Graphics grPhoto = Graphics.FromImage(bitPhoto); grPhoto.SmoothingMode = SmoothingMode.AntiAlias; grPhoto.DrawImage(gPhoto, new Rectangle(0, 0, PhotoWidth, PhotoHeight), 0, 0, PhotoWidth, PhotoHeight, GraphicsUnit.Pixel); Font crFont = new Font("楷体", iFontSize, FontStyle.Bold); SizeF crSize = grPhoto.MeasureString(strCopyright, crFont); //设置字体在图片中的位置 float yPosFromBottom = PhotoHeight - iFontButtomSpace - (crSize.Height); //float xCenterOfImg = (phWidth/2); float xCenterOfImg = PhotoWidth - iFontRightSpace - (crSize.Width / 2); //设置字体居中 StringFormat StrFormat = new StringFormat(); StrFormat.Alignment = StringAlignment.Center; //设置绘制文本的颜色和纹理 (Alpha=153) SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(this.iFontDiaphaneity, 0, 0, 0)); //将版权信息绘制到图象上 grPhoto.DrawString(strCopyright, crFont, semiTransBrush2, new PointF(xCenterOfImg, yPosFromBottom), StrFormat); gPhoto = bitPhoto; grPhoto.Dispose(); } if (bShowMarkImage) { //创建一个需要填充水银的Image对象 Image imgWater = new Bitmap(strMarkPath); int iMarkWidth = imgWater.Width; int iMarkmHeight = imgWater.Height; Graphics grWater = null; if (bShowCopyright) { //在原来修改过的bmPhoto上创建一个水银位图 bmWater = new Bitmap(bitPhoto); bmWater.SetResolution(gPhoto.HorizontalResolution, gPhoto.VerticalResolution); } else { bmWater = new Bitmap(gPhoto); } //将位图bmWater加载到Graphics对象 grWater = Graphics.FromImage(bmWater); ImageAttributes imageAttributes = new ImageAttributes(); ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float[][] colorMatrixElements = { new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, (float)iDiaphaneity/100f, 0.0f}, new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}}; ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); grWater.DrawImage(imgWater, new Rectangle((PhotoWidth - iMarkRightSpace - (iMarkWidth / 2)), (PhotoHeight - iMarkButtomSpace - (iMarkmHeight / 2)), iMarkWidth, iMarkmHeight), 0, 0, iMarkWidth, iMarkmHeight, GraphicsUnit.Pixel, imageAttributes); gPhoto = bmWater; grWater.Dispose(); imgWater.Dispose(); } gPhoto.Save(strSavePath, ImageFormat.Jpeg); } finally { if (bitPhoto != null) bitPhoto.Dispose(); if (bmWater != null) bmWater.Dispose(); gPhoto.Dispose(); } } }效果确是这样的:原图:相框: this.iDiaphaneity = 50;//图片水印透明度 这个数 怎么调都不对,要么中间一片黑,要么 相框图也是透明的才能看见里面的原图。 边框可以是整张中间透明或者半透明的png图片。也可以是多张图片的组合编排。最后用gdi+绘制到原图上去。 private Image ReGrahp(string sourcefile) { Image SourceImage = Image.FromFile(sourcefile); Image bmp = new System.Drawing.Bitmap(@"C:\Users\Administrator\Pictures\143097_1320710793tp40.jpg");//这里是背景图片 Size size = new Size(new Point(bmp.Width-58, bmp.Height-50));//这个是源图片的加载宽度和高度 Graphics g = Graphics.FromImage(bmp); g.DrawImage(SourceImage, new Rectangle(28,30, size.Width, size.Height), 10, 10, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel);//Rectangle为 30距离顶部和 28左边的距离。 return bmp; } 获得cpu温度问题? C# 调用C++写的dll 报错 看看这段代码有什么问题 为什么说没有包含Update的定义 关于Word邮件合并的问题, 有没有这样的视图 求助!! 求助这样的代码编辑器该怎么实现 richtextBox里的rtf格式插入一段文字 这是vs.net 2003的bug吗? 高分请教,指点即可,高手一句话,即可得100分!来者有分!!! 线程池里面的Timer 好机器上winform DataGridView数据慢
然后里边放你的照片picturebox,拉伸到适当大小。再将PictureBox的Anchor设为Top, Bottom, Left, Right
再放一个图片入其中,将两个尺寸设成一样,图片的定位点设为0,0,即可
/// WaterMark 的摘要说明
/// </summary>
///
/// <param name="strCopyright">要加入的文字</param>
/// <param name="strMarkPath">水印图片路径</param>
/// <param name="strPhotoPath">要加水印的图片路径</param>
/// <param name="strSavePath">处理后的图片路径</param>
/// <param name="iMarkRightSpace">水印在修改图片中距左边的宽度</param>
/// <param name="iMarkButtomSpace">水印在修改图片中距底部的高度</param>
/// <param name="iDiaphaneity">水印图片的透明度</param>
/// <param name="iFontRightSpace">文字</param>
/// <param name="iFontButtomSpace">文字</param>
/// <param name="iFontDiaphaneity">文字</param>
/// <param name="bShowCopyright">是否显示文字</param>
/// <param name="bShowMarkImage">是否显示水印图片</param> public class WaterMark
{
#region param
private string strCopyright, strMarkPath, strPhotoPath, strSavePath;
private int iMarkRightSpace, iMarkButtomSpace, iDiaphaneity;
private int iFontRightSpace = 0, iFontButtomSpace = 0, iFontDiaphaneity = 80;
private int iFontSize = 10;
private bool bShowCopyright = true, bShowMarkImage = true;
#endregion #region WaterMark
public WaterMark()
{
this.strCopyright = "";
this.strMarkPath = null;
this.strPhotoPath = null;
this.strSavePath = null;
this.iDiaphaneity = 50;//图片水印透明度
this.iMarkRightSpace = 0;
this.iMarkButtomSpace = 0;
} /// <summary>
/// 主要用两样都加的
/// </summary>
public WaterMark(string copyright, string Path, string photoPath, string savePath,int _iDiaphaneity)
{
this.strCopyright = copyright;
this.strMarkPath = Path;
this.strPhotoPath = photoPath;
this.strSavePath = savePath;
this.iDiaphaneity = _iDiaphaneity;//图片水印透明度
this.iMarkRightSpace = 0;
this.iMarkButtomSpace = 0;
}
#endregion #region property /// <summary>
/// 设置是否显示水印文字
/// </summary>
public bool ShowCopyright
{
set { this.bShowCopyright = value; }
} /// <summary>
/// 设置是否显示水印图片
/// </summary>
public bool ShowMarkImage
{
set { this.bShowMarkImage = value; }
}
/// <summary>
/// 获取或设置要加入的文字
/// </summary>
public string Copyright
{
set { this.strCopyright = value; }
} /// <summary>
/// 获取或设置加水印后的图片路径
/// </summary>
public string SavePath
{
get { return this.strSavePath; }
set { this.strSavePath = value; }
} /// <summary>
/// 获取或设置水印图片路径
/// </summary>
public string MarkPath
{
get { return this.strMarkPath; }
set { this.strMarkPath = value; }
} /// <summary>
/// 获取或设置要加水印图片的路径
/// </summary>
public string PhotoPath
{
get { return this.strPhotoPath; }
set { this.strPhotoPath = value; }
} /// <summary>
/// 设置水印图片的透明度
/// </summary>
public int Diaphaneity
{
set
{
if (value > 0 && value <= 100)
this.iDiaphaneity = value;
}
} /// <summary>
/// 设置水印字体的透明度0-255
/// </summary>
public int FontDiaphaneity
{
set
{
if (value >= 0 && value <= 255)
this.iFontDiaphaneity = value;
}
} /// <summary>
/// 设置水印图片在修改图片中距左边的高度
/// </summary>
public int MarkRightSpace
{
set { this.iMarkRightSpace = value; }
} /// <summary>
/// 设置水印图片在修改图片中距底部的高度
/// </summary>
public int MarkButtomSpace
{
set { this.iMarkButtomSpace = value; }
} /// <summary>
/// 设置水印字体在修改图片中距左边的距离
/// </summary>
public int FontRightSpace
{
set { iFontRightSpace = value; }
} /// <summary>
/// 设置水印字体在修改图片中距底部的高度
/// </summary>
public int FontButtomSpace
{
set { iFontButtomSpace = value; }
} #endregion
/// <summary>
/// 生成水印图片
/// </summary>
/// <returns></returns>
public void createMarkPhoto()
{
Bitmap bmWater = null;
Image gPhoto = Image.FromFile(this.strPhotoPath);
int PhotoWidth = gPhoto.Width;
int PhotoHeight = gPhoto.Height;
Bitmap bitPhoto = new Bitmap(PhotoWidth, PhotoHeight, PixelFormat.Format24bppRgb);
bitPhoto.SetResolution(gPhoto.HorizontalResolution, gPhoto.VerticalResolution); try
{
if (bShowCopyright)
{
Graphics grPhoto = Graphics.FromImage(bitPhoto);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.DrawImage(gPhoto, new Rectangle(0, 0, PhotoWidth, PhotoHeight), 0, 0, PhotoWidth, PhotoHeight, GraphicsUnit.Pixel); Font crFont = new Font("楷体", iFontSize, FontStyle.Bold);
SizeF crSize = grPhoto.MeasureString(strCopyright, crFont); //设置字体在图片中的位置
float yPosFromBottom = PhotoHeight - iFontButtomSpace - (crSize.Height); //float xCenterOfImg = (phWidth/2);
float xCenterOfImg = PhotoWidth - iFontRightSpace - (crSize.Width / 2);
//设置字体居中 StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center; //设置绘制文本的颜色和纹理 (Alpha=153)
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(this.iFontDiaphaneity, 0, 0, 0)); //将版权信息绘制到图象上
grPhoto.DrawString(strCopyright, crFont, semiTransBrush2, new PointF(xCenterOfImg, yPosFromBottom), StrFormat); gPhoto = bitPhoto;
grPhoto.Dispose();
} if (bShowMarkImage)
{
//创建一个需要填充水银的Image对象
Image imgWater = new Bitmap(strMarkPath);
int iMarkWidth = imgWater.Width;
int iMarkmHeight = imgWater.Height; Graphics grWater = null;
if (bShowCopyright)
{
//在原来修改过的bmPhoto上创建一个水银位图
bmWater = new Bitmap(bitPhoto);
bmWater.SetResolution(gPhoto.HorizontalResolution, gPhoto.VerticalResolution);
}
else
{
bmWater = new Bitmap(gPhoto);
} //将位图bmWater加载到Graphics对象
grWater = Graphics.FromImage(bmWater);
ImageAttributes imageAttributes = new ImageAttributes(); ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, (float)iDiaphaneity/100f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};
ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); grWater.DrawImage(imgWater, new Rectangle((PhotoWidth - iMarkRightSpace - (iMarkWidth / 2)), (PhotoHeight - iMarkButtomSpace - (iMarkmHeight / 2)), iMarkWidth, iMarkmHeight), 0, 0, iMarkWidth, iMarkmHeight, GraphicsUnit.Pixel, imageAttributes); gPhoto = bmWater;
grWater.Dispose();
imgWater.Dispose();
}
gPhoto.Save(strSavePath, ImageFormat.Jpeg);
}
finally
{ if (bitPhoto != null)
bitPhoto.Dispose(); if (bmWater != null)
bmWater.Dispose(); gPhoto.Dispose();
} }
}
效果确是这样的:原图:相框:
这个数 怎么调都不对,要么中间一片黑,要么 相框图也是透明的才能看见里面的原图。
{
Image SourceImage = Image.FromFile(sourcefile);
Image bmp = new System.Drawing.Bitmap(@"C:\Users\Administrator\Pictures\143097_1320710793tp40.jpg");//这里是背景图片
Size size = new Size(new Point(bmp.Width-58, bmp.Height-50));//这个是源图片的加载宽度和高度 Graphics g = Graphics.FromImage(bmp);
g.DrawImage(SourceImage, new Rectangle(28,30, size.Width, size.Height), 10, 10, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel);//Rectangle为 30距离顶部和 28左边的距离。
return bmp;
}