改变图片的大小关键代码
public static Bitmap ChangeSize(string path){
Bitmap bitMap = new Bitmap(351, 240);
Image img = Image.FromFile(path);
Graphics gs = Graphics.FromImage((Image)bitMap);
gs.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gs.DrawImage(img,0,0,351,240);
gs.Dispose();
return bitMap;
}
使用binaryreader和binarywrite读写一张图片,同时通过加入你想要的字符,从而加密图片
public static void EncryptionImage(string path, string encryptionStr)
{
byte[] en = Encoding.Default.GetBytes(encryptionStr);
string temp = path;
FileStream inFs = new FileStream(path, FileMode.Open, FileAccess.Read);
path = path.Substring(0, path.IndexOf('.')) + "_加密" + path.Substring(path.IndexOf('.'));
FileStream outFs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
byte[] buf = new byte[inFs.Length];
BinaryReader br = new BinaryReader(inFs);
br.Read(buf, 0, buf.Length);
BinaryWriter bw = new BinaryWriter(outFs);
for (int i = 0; i < buf.Length; i++)
{
bw.Write(buf[i]);
if (i % 50 == 0)
{
bw.Write(encryptionStr);
}
}
bw.Flush();
bw.Close();
outFs.Close();
br.Close();
inFs.Close();
}
截图如下:


解密的功能是加密的相反过程,这就不写了。
附代码:链接:https://pan.baidu.com/s/1lxYBtqQD1Rynt0lyZ901JA 提取码:rsrz
本文分享了如何使用C#代码调整图片尺寸及加密图片的方法。详细介绍了改变图片大小的关键代码,并演示了如何通过在图片中加入特定字符来实现图片加密,提供了完整的代码示例。
1128

被折叠的 条评论
为什么被折叠?



