日期:2014-05-18  浏览次数:21209 次

C# winform 本地选择图片把路径上传到数据库,怎么把绝对地址装换成相对地址?
C# winform 本地选择图片把路径上传到数据库,怎么把绝对地址装换成相对地址?
在picturebox下面有个选择本地的按钮,将在本地选择的图片显示在picturebox里面,将路径保存到数据库中,但保存的是绝对地址,怎么换成相对地址呢?

------解决方案--------------------
截取字符串。。
------解决方案--------------------

(1)复制该文件到image (C# 把文件拷贝到指定文件夹 )

if(!System.IO.Directory.Exists(@"d:\你程序\imgage")) 

// 目录不存在,建立目录 
System.IO.Directory.CreateDirectory(@"d:\你程序\imgage"); 
}

String sourcePath = "e:\\源文件目录\\图片文件";

String targetPath = "d:\你程序\imgage";

bool isrewrite=true; // true=覆盖已存在的同名文件,false则反之

System.IO.File.Copy(sourcePath, targetPath, isrewrite);



(2)得到本地路径文件
 string fullNameNotExtName = System.Windows.Forms.Application.StartupPath.ToString() + "\\images" + 图片名称;
------解决方案--------------------
选择文件的过程:
private void rmiAddPics_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "JPG files (*.JPG)|*.JPG|BMP files (*.BMP)|*.BMP" ;
openFileDialog.FilterIndex = 1 ;

string strFile ="";
string strDiy = "";
if(openFileDialog.ShowDialog() == DialogResult.OK)
{
strFile=openFileDialog.FileName.ToString();

string fullNameNotExtName=System.IO.Path.GetFileNameWithoutExtension(strFile);

strDiy=System.IO.Path.GetDirectoryName(strFile);
//FileInfo fi = new FileInfo(strFile);

System.Drawing.Image image = Image.FromFile(strFile);

this.pictureBox1.Image=image;


MemoryStream ms = new MemoryStream();
image.Save(ms,image.RawFormat);
byte[] serializedObject = ms.GetBuffer();

Imgname=this.comboBox1.Text=fullNameNotExtName.Trim();
Imgpath=strFile.Trim();
Imgcode=serializedObject;

ms.Close();

this.comboBox1 .Enabled=false;


openFileDialog.OpenFile().Close();
openFileDialog.OpenFile().Flush();
}
}

------解决方案--------------------
为什么不用流直接存进数据库呢?