将byte []转换为图像 [英] Converting byte[] To Image

查看:76
本文介绍了将byte []转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用Flash工具从ASP.NET页面使用摄像头捕获图像.

捕获后,我将其存储在数据库中.
图片的大小约为40kb.
我正在从数据库中检索图像以在asp.net页中查看.

检索到的图像与捕获的图像相同.

我想要的是图像的宽度应减小到所需的宽度.

这是我的代码...

Hi all,

I am using a flash tool to capture an image using webcam from asp.net page.

After capturing, I am storing it on a database.
The size of the image is around 40kb.
I am retrieving the image from the database to view in an asp.net page.

The image retrieved is same as that of the captured image.

What I want is that the width of the image should be reduced to a required width.

This is my code...

byte[] im = (byte[])dt.Rows[0]["Photo"]; //How to Convert this im  to srcimage

                        Bitmap newImage = new Bitmap(10, 10);
           using (Graphics gr = Graphics.FromImage(newImage))
           {
               gr.SmoothingMode = SmoothingMode.AntiAlias;
               gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
               gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
               gr.DrawImage(srcImage, new Rectangle(0, 0, 10, 10));

           }

           Response.ContentType = path;
           Response.BinaryWrite(im);



在此先感谢您.



Thanks in advance.

推荐答案

对于所编辑的内容:

创建一个MemoryStream并使用 Image.FromStream [
For the edited in question:

Create a MemoryStream and use Image.FromStream[^].


这不是压缩" .如果您存储的是JPEG或其他有损或无损压缩文件,则可能存储了已压缩的图像.您需要的被称为重新采样".您需要了解的第一件事:您可以以可接受的质量有效缩小图片尺寸.如果您想通过扩大来重新采样,请不要希望质量高.我认为这很明显.

您可以执行以下两项操作之一:使用C#重新采样图像或使用属性width:
照原样在页面上使用它
This is not "compressing". Probably you store already compressed image, if you store JPEG or something else with lossy or lostless compression. What you need is called "re-sampling". First thing you need to know: you can effectively reduce picture size with acceptable quality. If you want to re-sample with enlargement, don''t hope for good quality. I think this is obvious.

You can do one of two things: re-sample the image in C# or use it on the page as is using the attribute width:

<img src="myfile.jpg" width="required width in pixels" />



在这种情况下,图像将通过Web浏览器重新采样.这种方法的问题是,将以完整尺寸下载图像,与较小的图像尺寸相比,它需要更多的时间并使用更多的流量.不过,我认为这种最简单的方法在大多数实际情况下都是最好的.如果原始图像确实很大,或者您以不同的分辨率(缩放功能)显示巨大的图片的不同片段,则可能需要在服务器端进行重新采样.

要使用C#在服务器端进行重新采样,使用System.Drawing非常容易.例如,请参见以下代码示例:
http://stackoverflow.com/questions /87753/resizing-an-image-without-losing-any-quality [ ^ ].



使用类Bitmap及其构造函数Bitmap(Stream).使用构造函数MemoryStream(Byte[])从字节创建内存流,并从此内存流中读取位图:



In this case, the image will be re-sampled by the Web browser. The problem of this approach is that the image will be downloaded in its full size, which needs more time and uses more traffic compared to the smaller image size. Still, I think this simplest method the best in most practical cases. You might need re-sampling on the server side if the original image is really much bigger, or you show different fragment of huge picture with different resolutions (zoom feature).

For re-sampling in on the server side in C# is pretty easy with System.Drawing. See, for example, this code sample: http://stackoverflow.com/questions/87753/resizing-an-image-without-losing-any-quality[^].



Use the class Bitmap and its constructor Bitmap(Stream). Create memory stream from your bytes using constructor MemoryStream(Byte[]) and read bitmap from this memory stream:

Bitmap myBitmap = new Bitmap(new MemoryStream(myBytes));



请参阅 http://msdn.microsoft.com/en-us/library/system.drawing .bitmap.aspx [ ^ ], http://msdn.microsoft.com/en-us /library/system.drawing.bitmap.aspx [ ^ ].

-SA



See http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx[^], http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx[^].

—SA


如果您有图像,则可以创建所需大小的新空白图像,并将调整大小后的图像绘制到该图像中图形对象,然后以该新大小保存图像.实际上,我觉得您可以将图像和大小传递给图像构造函数以执行相同的操作.
If you have an image, you can create a new, blank image in the size you want, and draw your resized image on to that image within a Graphics object, and then save the image in that new size. In fact, I feel you can pass an image and a size in to an image constructor to do the same thing.


这篇关于将byte []转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆