Exemplo n.º 1
0
        private long SendImageCore(LanUser user, Image image, string imagePath)
        {
            string path;

            if (image != null)
            {
                path = LanConfig.Instance.GetTempFileName(".png");
                image.Save(path, ImageFormat.Png);
            }
            else
            {
                path = imagePath;
            }

            long len = LanFile.GetFileLength(path);

            if (len > UdpClientEx.UDP_MAX_BUF_SIZE)
            {
                //图像文件过大的话用文件形式发送
                return(SendFile(user, path, true));
            }
            else
            {
                UdpPacket packet = new UdpPacket();
                packet.Address = user.IP;
                packet.Port    = user.Port;
                packet.ToMAC   = user.MAC;
                packet.Command = UdpPacket.CMD_SEND_IMAGE | UdpPacket.CMD_OPTION_NEED_RESPONSE;
                packet.FromMAC = this.MAC;

                UdpPacketImageExtend extend = new UdpPacketImageExtend();
                extend.EncryptKey = user.SecurityKeys.Public;
                extend.Image      = image ?? Image.FromFile(path);
                packet.Extend     = extend;

                _client.Send(packet);

                if (image != null)
                {
                    //直接发送图像的话,临时图片就删除
                    LanFile.Delete(path);
                }

                return(packet.ID);
            }
        }
Exemplo n.º 2
0
        public static void SetPhoto(string key, Image img)
        {
            string fileName = Path.Combine(LanConfig.Instance.ProfilePhotoPath, key);

            _cache.Remove(key);
            LanFile.Delete(fileName);

            if (img == null)
            {
                return;
            }

            using (Bitmap bmp = new Bitmap(img.Width, img.Height))
            {
                bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);

                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.DrawImageUnscaled(img, 0, 0);
                }
                bmp.Save(fileName, ImageFormat.Jpeg);
            }
        }