You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure FileShare图片裁剪遇非法字符错误求助(ASP.NET/VB.NET)

问题原因

SD.Image.FromFile仅支持本地文件系统路径,你传入的是带SAS令牌的Azure FileShare URL,这不属于合法本地路径,因此触发“Illegal characters in path”错误,和URL编码/转义无关。

解决方案:使用Azure.Storage.Files.Shares处理图片

通过Azure文件共享SDK将文件下载到内存流,再从内存流读取图片进行裁剪,避免直接使用URL作为文件路径。

步骤1:安装NuGet包

首先安装Azure文件共享的SDK包:

Install-Package Azure.Storage.Files.Shares

步骤2:完整VB.NET代码示例

Imports Azure.Storage.Files.Shares
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.IO

Public Function CropAzureFileShareImage(fileShareName As String, filePath As String, sasToken As String, cropWidth As Integer, cropHeight As Integer, cropX As Integer, cropY As Integer) As Byte()
    ' 初始化文件共享客户端
    Dim shareClient As New ShareClient($"https://<你的存储账户名>.file.core.windows.net/{fileShareName}{sasToken}")
    Dim fileClient = shareClient.GetRootDirectoryClient().GetFileClient(filePath)

    ' 将文件下载到内存流
    Using downloadStream As New MemoryStream()
        fileClient.DownloadTo(downloadStream)
        downloadStream.Position = 0 ' 重置流指针到起始位置

        ' 从内存流读取原始图片
        Using originalImage As Image = Image.FromStream(downloadStream)
            ' 创建裁剪后的位图
            Using croppedBmp As New Bitmap(cropWidth, cropHeight)
                croppedBmp.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution)

                ' 绘制裁剪内容
                Using graphic As Graphics = Graphics.FromImage(croppedBmp)
                    graphic.SmoothingMode = SmoothingMode.AntiAlias
                    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic
                    graphic.PixelOffsetMode = PixelOffsetMode.HighQuality
                    graphic.DrawImage(originalImage, New Rectangle(0, 0, cropWidth, cropHeight), cropX, cropY, cropWidth, cropHeight, GraphicsUnit.Pixel)

                    ' 将裁剪后的图片写入内存流并返回字节数组
                    Using resultStream As New MemoryStream()
                        croppedBmp.Save(resultStream, originalImage.RawFormat)
                        Return resultStream.ToArray() ' 用ToArray()代替GetBuffer(),避免多余空字节
                    End Using
                End Using
            End Using
        End Using
    End Using
End Function

' 可选:将裁剪后的图片上传回Azure FileShare
Public Sub UploadCroppedImageToFileShare(fileShareName As String, targetFilePath As String, sasToken As String, croppedImageBytes As Byte())
    Dim shareClient As New ShareClient($"https://<你的存储账户名>.file.core.windows.net/{fileShareName}{sasToken}")
    Dim fileClient = shareClient.GetRootDirectoryClient().GetFileClient(targetFilePath)

    Using uploadStream As New MemoryStream(croppedImageBytes)
        fileClient.Upload(uploadStream, overwrite:=True)
    End Using
End Sub

关键说明

  • 替换代码中的<你的存储账户名>为实际的Azure存储账户名称
  • 使用DownloadTo将文件下载到内存流,避免写入本地磁盘
  • 用resultStream.ToArray()代替GetBuffer(),因为GetBuffer()会返回内存流的整个缓冲区(包含未使用的空字节),ToArray()只返回实际的图片字节
  • 如果需要将裁剪后的图片保存回FileShare,调用UploadCroppedImageToFileShare方法即可

内容的提问来源于stack exchange,提问作者Okta Canvas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 13:27:34