开发者

NEED HELP.crop image while streaming it from IP cam

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(开发者_Python百科);
        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


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜