使用MVC应用程序上传图像,我们需要在保存到数据库之前减小文件大小,但是想要保留EXIF数据......我能想到的唯一方法是从原始上传的图像中获取这个并且然后使用 MetadataExtractor 将其添加到调整大小的那个
我们可以像这样获取元数据
Dim vMetadata As IEnumerable(Of System.IO.Directory) = MetadataExtractor.ImageMetadataReader.ReadMetadata(file.InputStream)
然后像这样调整图像大小
fext = IO.Path.GetExtension(file.FileName).ToLower
Dim vLen As Integer = file.ContentLength
Dim vData(vLen - 1) As Byte
Dim image_file As System.Drawing.Image = System.Drawing.Image.FromStream(file.InputStream)
Dim image_height As Integer = image_file.Height
Dim image_width As Integer = image_file.Width
Dim max_height As Integer = 240
Dim max_width As Integer = 320
image_height = (image_height * max_width) / image_width
image_width = max_width
If image_height > max_height Then
image_width = (image_width * max_height) / image_height
image_height = max_height
End If
Dim bitmap_file As New System.Drawing.Bitmap(image_file, image_width, image_height)
Using vStream As New IO.MemoryStream
Select Case fext
Case ".jpg"
bitmap_file.Save(vStream, System.Drawing.Imaging.ImageFormat.Jpeg)
Case ".jpeg"
bitmap_file.Save(vStream, System.Drawing.Imaging.ImageFormat.Jpeg)
Case ".png"
bitmap_file.Save(vStream, System.Drawing.Imaging.ImageFormat.Png)
Case ".gif"
bitmap_file.Save(vStream, System.Drawing.Imaging.ImageFormat.Gif)
Case Else
bitmap_file.Save(vStream, System.Drawing.Imaging.ImageFormat.Jpeg)
End Select
vStream.Position = 0
vStream.Read(vData, 0, vStream.Length)
vImageFile = vData
vData = Nothing
End Using
因此,我们将元数据保存为字典,将缩小尺寸的图像文件保存为变量 vImageFile,我们现在可以保存...
问题是 - 我们如何将元数据添加回新文件?
谢谢
-------------------- 编辑添加于 2017 年 10 月 1 日 --------------------
我已添加此代码
Dim vOrientationNumber As Integer = 1
Dim vEXIF As String = ""
Dim vDirectories = ImageMetadataReader.ReadMetadata(file.InputStream)
Dim vSub = vDirectories.OfType(Of ExifSubIfdDirectory)().FirstOrDefault
If Not vSub Is Nothing Then
Dim vOrientationObj = vSub.GetObject(ExifDirectoryBase.TagOrientation)
If Not vOrientationObj Is Nothing Then
If Not vOrientationObj.Equals(DBNull.Value) Then
vOrientationNumber = Convert.ToInt16(vOrientationObj)
End If
End If
End If
For Each vDirectory In vDirectories
For Each Tag In vDirectory.Tags
vEXIF += vDirectory.Name & " " & Tag.Name & " " & Tag.Description & Environment.NewLine
Next
Next
获取方向编号的句柄,但变量 vSub 始终为 Nothing。我知道这张图片的方向号在那里(因为它在主 WPF 桌面应用程序中找到它并旋转它)。知道我现在做错了什么吗?