我想将已加载的图片保存到某个目录,但它会一直返回无效的导演。这是我正在使用的代码
if (OpenCdb != null)
{
string pics = Path.GetDirectoryName(OpenCdb.FileName);
// Save card image
ImageResizer.SaveImage(CardImg.Image, pics + "\\pics\\" + cardid + ".jpg", 177, 254);
//Save card thumbnail
ImageResizer.SaveImage(CardImg.Image, pics + "\\pics\\thumbnail\\" + cardid + ".jpg", 44, 64);
}
我测试了pics
字符串并返回了正确的目录,所以我不知道还能做什么。我也可以发布imageresizer功能代码,如果你也需要我,但它是很多代码,这就是为什么我没有在问题中添加它。
编辑继承人错误的图片
public static void SaveImage(Image image, string savedName, int width = 0, int height = 0)
{
Image originalImage = image;
string filePath = AppDomain.CurrentDomain.BaseDirectory + savedName;
if (width > 0 && height > 0)
{
Image.GetThumbnailImageAbort myCallback =
new Image.GetThumbnailImageAbort(ThumbnailCallback);
Image imageToSave = originalImage.GetThumbnailImage
(width, height, myCallback, IntPtr.Zero);
imageToSave.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
originalImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
答案 0 :(得分:3)
您尝试保存的路径是这样构成的:
AppDomain.CurrentDomain.BaseDirectory + Path.GetDirectoryName(OpenCdb.FileName) + "\\pics\\" + cardid + ".jpg"
结果看起来很像“C:\ blahC:\ meh \ pics \ something.jpg”
答案 1 :(得分:2)
我猜你的一个或多个字符串有问题。
Path.Combine(pics, "pics", cardid + ".jpg")
和Path.Combine(AppDomain.CurrentDomain.BaseDirectory, savedName)
另外,我会在使用之前将整个路径组合起来,如:
string fullPath = Path.Combine(pics, "pics", cardid + ".jpg")
<强>更新强>
如下所述,路径混淆了.. 你设置这样的照片:
string pics = Path.GetDirectoryName(OpenCdb.FileName);
这将导致pics = C:\something\something
然后你这样打电话给SaveImage
:
SaveImage(CardImg.Image, pics + "\\pics\\" + cardid + ".jpg", 177, 254);
在该方法中,您现在设置pics = savedName
,然后完整路径为:
string filePath = AppDomain.CurrentDomain.BaseDirectory + savedName;
哪个是C:\ somethingelse \ C:\ something \ something
答案 2 :(得分:1)
哇我再次创造了这种情况并找回了解决方案
解决方案就在这里
第1步:(不需要,但更好的预防方式)
添加Directory.CreateDirectory(Path.GetDirectoryName(filePath));保存图像如下
public static void SaveImage(Image image, string savedName, int width = 0, int height = 0)
{
Image originalImage = image;
string filePath = AppDomain.CurrentDomain.BaseDirectory + savedName;
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
.
.
. The Rest code
.
.
.
.
.
.
.
}
第2步:
if (OpenCdb != null)
{
string pics ="";// no need of this (this is the problem) Path.GetDirectoryName(OpenCdb.FileName);
Path.GetDirectoryName(OpenCdb.FileName);将返回类似C:***的东西你再次与SaveImage中的AppDomain.CurrentDomain.BaseDirectory组合这是问题
// Save card image
ImageResizer.SaveImage(CardImg.Image, pics + "\\pics\\" + cardid + ".jpg", 177, 254);
//Save card thumbnail
ImageResizer.SaveImage(CardImg.Image, pics + "\\pics\\thumbnail\\" + cardid + ".jpg", 44, 64);
}
答案 3 :(得分:0)
永远不要让我在这里工作的每个人都是我最终使用的代码
public static void SaveImage(Image image, string savedName, int width = 0, int height = 0)
{
Image originalImage = image;
string filePath = savedName;
if (width > 0 && height > 0)
{
Image.GetThumbnailImageAbort myCallback =
new Image.GetThumbnailImageAbort(ThumbnailCallback);
Image imageToSave = originalImage.GetThumbnailImage
(width, height, myCallback, IntPtr.Zero);
imageToSave.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
originalImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
...
public void SaveImage(string cardid)
{
if (LoadedImage != "")
{
if (OpenCdb != null)
{
string pics = Path.GetDirectoryName(OpenCdb.FileName);
// Save card image
ImageResizer.SaveImage(CardImg.Image, pics + "\\pics\\" + cardid + ".jpg", 177, 254);
//Save card thumbnail
ImageResizer.SaveImage(CardImg.Image, pics + "\\pics\\thumbnail\\" + cardid + ".jpg", 44, 64);
}
else
{
string pics = AppDomain.CurrentDomain.BaseDirectory;
// Save card image
ImageResizer.SaveImage(CardImg.Image,
pics + "pics\\" + cardid + ".jpg", 177, 254);
//Save card thumbnail
ImageResizer.SaveImage(CardImg.Image,
pics + "pics\\thumbnail\\" + cardid + ".jpg", 44, 64);
}
}
}