使用 ASP.NET 调整图像大小并保存到数据库
我需要拍摄上传的图像,调整其大小,然后将其保存到数据库中。很简单,只是我无权将任何临时文件保存到服务器。我正在拍摄图像,将其大小调整为位图,并需要将其作为原始图像类型(例如 JPG)保存到数据库字段中。我怎样才能像这样获得 FileBytes() ,以便将其保存到数据库中?
在我使用 ImageUpload.FileBytes() 之前,但现在我正在调整大小,我正在处理图像和位图而不是 FileUploads,并且似乎找不到任何可以给我字节的东西。
谢谢!
I need to take an uploaded image, resize it, and save it to the database. Simple enough, except I don't have access to save any temp files to the server. I'm taking the image, resizing it as a Bitmap, and need to save it to a database field as the original image type (JPG for example). How can I get the FileBytes() like this, so I can save it to the database?
Before I was using ImageUpload.FileBytes() but now that I'm resizing I'm dealing with Images and Bitmaps instead of FileUploads and can't seem find anything that will give me the bytes.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上没那么简单...有 28 个不明显的陷阱您应该注意调整图像大小时。最好使用我的免费开源库来处理所有编码问题并避免 GDI 错误。
以下是如何获取调整大小、裁剪并转换为 Jpeg 格式后,每个上传文件的编码 byte[] 数组。
使用 MS SQL 存储图像也是一个坏主意。请参阅我的 斯科特·汉塞尔曼的播客了解更多信息。
It's actually not so simple... there are 28 non-obvious pitfalls you should watch out for when doing image resizing. It's best to use my free, open-source library to handle all the encoding issues and avoid the GDI bugs.
Here's how to get an encoded byte[] array for each uploaded file, after resizing, cropping, and converting to Jpeg format.
It's also a bad idea to use MS SQL for storing images. See my podcast with Scott Hanselman for more info.
请参阅在不损失任何质量的情况下调整图像大小,然后您可以编写图像(Bitmap.SaveToStream) 到 MemoryStream 并调用 ToArray 来获取字节。
See Resizing an Image without losing any quality You can then write your image (Bitmap.SaveToStream) to a MemoryStream and call ToArray to get the bytes.
这就是我调整图像大小所做的事情。
This is what I've done to resize images.