从IP cam流式传输时需要HELP.crop图像

时间:2010-11-07 13:22:21

标签: c# image streaming

我有一个监控系统,我想在报警触发时保存来自摄像头的快照。我已经尝试了很多方法来做到这一点......而且一切正常。

string ImageName = @"E:\snapshot\pic" + imageid + ".jpg";
WebClient webclient = new WebClient();
webclient.Credentials = new NetworkCredential("admin", "pass");
Uri url = new Uri("http://" + ip + "/cgi-bin/cmd/encoder?SNAPSHOT");
webclient.DownloadFileAsync(url, ImageName);
webclient.Dispose();

来自凸轮的图像是(1280 * 1024)。我想裁剪图像得到(500 * 500)Pixel

private void button2_Click(object sender, EventArgs e)
    {
        string ImageFrom = @"c:\3.jpg";
        byte[] imageData = ReadFile(ImageFrom);
        byte[] data = CropPicture(imageData, 500, 500);
        SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=test;Integrated Security=True");
        string qry = "insert into val (id,img) values (@OriginalPath, @ImageData)";
        SqlCommand SqlCom = new SqlCommand(qry, cn);
        SqlCom.Parameters.Add(new SqlParameter("@OriginalPath",(object)"123"));
        SqlCom.Parameters.Add(new SqlParameter("@ImageData", (object)data));
        cn.Open();
        SqlCom.ExecuteNonQuery();
    }
    byte[] ReadFile(string sPath)
    {
        byte[] data = null;
        FileInfo fInfo = new FileInfo(sPath);
        long numBytes = fInfo.Length;
        FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fStream);
        data = br.ReadBytes((int)numBytes);
        return data;
    }
    public static byte[] CropPicture(byte[] imgFile, int targetW, int targetH)
    {
        Image imgPhoto = Image.FromStream(new MemoryStream(imgFile));
        int targetX = (imgPhoto.Width - targetW) / 2;
        int targetY = (imgPhoto.Height - targetH) / 2;
        Bitmap bmpPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
        bmpPhoto.SetResolution(80, 60);
        Graphics gfxPhoto = Graphics.FromImage(bmpPhoto);
        gfxPhoto.SmoothingMode = SmoothingMode.AntiAlias;
        gfxPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
        gfxPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
        gfxPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), targetX, targetY, targetW, targetH, GraphicsUnit.Pixel);
        MemoryStream mm = new MemoryStream();
        bmpPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
        // Dispose of all the objects to prevent memory leaks
        imgPhoto.Dispose();
        bmpPhoto.Dispose();
        gfxPhoto.Dispose();
        return mm.GetBuffer();
    }

然后将其插入sql数据库 我有一个代码来裁剪图像。我知道如何将图像插入到SQL数据库中 但它都需要将图像作为pc中的文件读取。 我流式传输图像然后保存 然后得到它并裁剪它并将其插入数据库 请任何人告诉我如何获取流而不需要保存它

1 个答案:

答案 0 :(得分:0)

你应该可以使用这样的东西......(未经测试)

Image img = Image.FromStream((new WebClient()).OpenRead("a"));