需要帮助。在从 IP 摄像头流式传输图像时裁剪图像

发布于 2024-10-01 17:24:12 字数 2691 浏览 0 评论 0原文

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

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)像素

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 数据库 但这都需要将图像作为文件读取到电脑中。 我流式传输图像然后保存它 然后获取它并裁剪它并将其插入到数据库中 请任何人告诉我如何获取流而不需要保存它

I have a monitoring system and I want to save a snapshot from a camera when alarm trigger. I have tried many methods to do that…and it’s all working fine.

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();

the image coming from the cam is(1280*1024). i want to crop the image to get (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();
    }

then insert it in the sql database
i got a code to crop the image.and i know how to insert image into sql database
but it all need to read the image as a file in the pc.
i stream the image then save it
then get it and crop it and insert it into db
PLEASE can any one tell me how to get the stream without the need to save it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜夜流光相皎洁 2024-10-08 17:24:13

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

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

You should be able to use something like this... (not tested)

Image img = Image.FromStream((new WebClient()).OpenRead("a"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文