Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在保存c时返回无效目录#_C# - Fatal编程技术网

C# 在保存c时返回无效目录#

C# 在保存c时返回无效目录#,c#,C#,我想将加载的图片保存到某个目录,但它始终返回无效的director。这是我正在使用的代码 if (OpenCdb != null) { string pics = Path.GetDirectoryName(OpenCdb.FileName); // Save card image ImageResizer.SaveImage(CardImg.Image, pics + "\\pics\\" + cardid + ".jpg", 177, 254); //Save

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

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函数代码,但是代码太多了,所以我没有在问题中添加它

这里有一张错误的图片


我猜你的一个或多个字符串有问题

  • 使用Path.Combine,例如
    Path.Combine(pics,“pics”,cardd+“.jpg”)
    Path.Combine(AppDomain.CurrentDomain.BaseDirectory,savedName)
  • 此外,在使用之前,我会组合整个路径,如:

    string fullPath=Path.Combine(pics,“pics”,cardd+“.jpg”)

    更新

    如下所述,路径正在混淆。。 您可以这样设置图片:

    string pics=Path.GetDirectoryName(OpenCdb.FileName)

    这将导致
    pics=C:\something\something

    然后你像这样调用
    SaveImage

    SaveImage(CardImg.Image,pics+“\\pics\\”+cardid+“.jpg”,177254)

    在该方法中,您现在设置
    pics=savedName
    ,完整路径将是:

    string filePath=AppDomain.CurrentDomain.BaseDirectory+savedName


    它是C:\somethingelse\C:\something\something

    我猜您的一个或多个字符串有问题

  • 使用Path.Combine,例如
    Path.Combine(pics,“pics”,cardd+“.jpg”)
    Path.Combine(AppDomain.CurrentDomain.BaseDirectory,savedName)
  • 此外,在使用之前,我会组合整个路径,如:

    string fullPath=Path.Combine(pics,“pics”,cardd+“.jpg”)

    更新

    如下所述,路径正在混淆。。 您可以这样设置图片:

    string pics=Path.GetDirectoryName(OpenCdb.FileName)

    这将导致
    pics=C:\something\something

    然后你像这样调用
    SaveImage

    SaveImage(CardImg.Image,pics+“\\pics\\”+cardid+“.jpg”,177254)

    在该方法中,您现在设置
    pics=savedName
    ,完整路径将是:

    string filePath=AppDomain.CurrentDomain.BaseDirectory+savedName


    它是C:\somethingelse\C:\something\something

    您试图保存到的路径是这样组成的:

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

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

    您试图保存到的路径是这样组成的:

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

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

    不管我在这里得到它的每个人都是我最终使用的代码

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


    哇,我又创造了局面,找到了解决方案

    解决方案就在这里

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

    添加Directory.CreateDirectory(Path.GetDirectoryName(filePath));如下图所示保存图像

    第二步:

    if (OpenCdb != null)
    {
        string pics ="";// no need of this (this  is the problem) Path.GetDirectoryName(OpenCdb.FileName);
    
    GetDirectoryName(OpenCdb.FileName);将返回类似于C:**的内容,您再次与SaveImage中的AppDomain.CurrentDomain.BaseDirectory组合,这就是问题所在


    哇,我又创造了局面,找到了解决方案

    解决方案就在这里

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

    添加Directory.CreateDirectory(Path.GetDirectoryName(filePath));如下图所示保存图像

    第二步:

    if (OpenCdb != null)
    {
        string pics ="";// no need of this (this  is the problem) Path.GetDirectoryName(OpenCdb.FileName);
    
    GetDirectoryName(OpenCdb.FileName);将返回类似于C:**的内容,您再次与SaveImage中的AppDomain.CurrentDomain.BaseDirectory组合,这就是问题所在


    “它一直返回无效目录”的确切含义是什么?什么返回什么值?什么是OpenCdb?您在此处提供的上下文不足。是否尝试使用
    Path.Combine()
    ?在使用断点调试时,错误发生在哪一行?您希望看到什么?你得到了什么。。。在哪里?用错误图片编辑“cardid”仅仅是一个数字标识符吗?它可能包含非法字符吗?你所说的“它一直返回无效目录”到底是什么意思?什么返回什么值?什么是OpenCdb?您在此处提供的上下文不足。是否尝试使用
    Path.Combine()
    ?在使用断点调试时,错误发生在哪一行?您希望看到什么?你得到了什么。。。在哪里?用错误图片编辑“cardid”仅仅是一个数字标识符吗?它可能包含非法字符吗?我刚刚测试过,返回的路径是正确的路径C:\Users\hp owner\Desktop\OutlawPro\pics\thumbnail\1234567890.jpgBut。。。在照片里。“文件路径”是BaseDirectory和savedName的串联。savedName是在代码示例中构建的,以OpenCdb.FileName的GetDirectoryName开头。@outlaw1994-从何处返回的路径?这是filePath还是savedName的值?或者这是连接它们的结果?路径是pics的值和我在图像中看到的代码的附加值now@Zec我从不恨D So+1我刚刚测试了它,这是返回的路径,是正确的路径C:\Users\hp owner\Desktop\OutlawPro\pics\thumbnail\1234567890.jpgBut。。。在照片里。“文件路径”是BaseDirectory和savedName的串联。savedName是在代码示例中构建的,以OpenCdb.FileName的GetDirectoryName开头。@outlaw1994-从何处返回的路径?这是filePath还是savedName的值?还是结果
        // 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);
    }