0

我想将加载的图片保存到某个目录,但它一直返回无效的导演。这是我正在使用的代码

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);
        }
    }
4

4 回答 4

3

您尝试保存的路径如下组成:

AppDomain.CurrentDomain.BaseDirectory + Path.GetDirectoryName(OpenCdb.FileName) + "\\pics\\" + cardid + ".jpg"

结果很有可能看起来像 "C:\blahC:\meh\pics\something.jpg"

于 2013-07-19T06:02:57.387 回答
2

我猜你的一根或多根琴弦有问题。

  1. 使用 Path.Combine 例如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

于 2013-07-19T05:58:07.050 回答
1

哇,我再次创造了这种情况并找到了解决方案

解决方案在这里

第 1 步:(不需要,但更好的预防方法)

添加 Directory.CreateDirectory(Path.GetDirectoryName(filePath)); SaveImage 如下

  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);
}
于 2013-07-19T06:33:36.173 回答
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);
            }
        }
    }
于 2013-07-19T06:27:09.510 回答