本文介绍了将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.

推荐答案


<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



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

08-28 12:20