asp.net抓取图片、保存成字节、文件夹、按固定区域截图

private void DownImage()
        {
            WebClient client = new WebClient();
            string url = @"http://image.weather.gov.cn/product/2012/201207/20120723/STFC/SEVP_NMC_STFC_SFER_ER6T06_ACHN_L88_P9_20120723060000606.JPG";
            byte[] data = client.DownloadData(url);
            string strtime = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒");
            string strPath = "~/Images/"+strtime + ".jpg";
            string smallPath = "~/Images/"+strtime +"-small"".jpg";
            SaveFile(data,  strPath);
            GetPartImage(681264, strPath,smallPath);
        } 

private void GetPartImage(int x,int y,string path,string smallPath)

        {
            Bitmap bm = new Bitmap(Server.MapPath(path));
            Rectangle cloneRect = new Rectangle(x,y, 100100);
            PixelFormat format = bm.PixelFormat;
            Bitmap cloneBitmap = bm.Clone(cloneRect, format);
            // ===保存图片===
            cloneBitmap.Save(Server.MapPath(smallPath), ImageFormat.Jpeg);
            bm.Dispose();
            cloneBitmap.Dispose();
        }
 private bool SaveFile(byte[] dates, string StrSavePath)
        {
            try
            {
                FileStream fs = new FileStream(Server.MapPath(StrSavePath), FileMode.Create, FileAccess.Write, FileShare.None);
                BinaryWriter bw = new BinaryWriter(fs);
                try
                {
                    bw.Write(dates);
                }
                catch (Exception e)
                {
                    return false;
                }
                finally
                {
                    bw.Close();
                    fs.Close();
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }

        } 

 

//写日志
private void WriteLog(string content)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "lograin";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string fileName = Path.Combine(path, DateTime.Now.ToString("yyyyMMdd") + ".txt");
            System.IO.FileStream stm = null;
            try
            {
                if (System.IO.File.Exists(fileName))
                {
                    stm = System.IO.File.OpenWrite(fileName);
                }
                else
                {
                    stm = System.IO.File.Create(fileName);
                }
                stm.Position = stm.Length;
                byte[] data = System.Text.Encoding.GetEncoding("gb2312").GetBytes(content + "\r\n");
                stm.Write(data, 0, data.Length);
                stm.Close();
            }
            catch
            {
                if (stm != null)
                {
                    stm.Close();
                }
            }
        }

 

//读取日志
string path = AppDomain.CurrentDomain.BaseDirectory + "lograin";
            string fileName = Path.Combine(path, DateTime.Now.ToString("yyyyMMdd") + ".txt");
            string[] _urlArrary = null;
            try
            {
                if (File.Exists(fileName))
                {
                    _urlArrary = System.IO.File.ReadAllLines(fileName, Encoding.GetEncoding("gb2312"));
                }
            }
            catch
            {
                _urlArrary = null;
            }

 

截图图片指定区域(Winform)
///<summary>
        
/// 获取图片指定部分
        
/// </summary>
        
/// <param name="pPath">图片路径</param>
        
/// <param name="pSavePath">保存路径</param>
        
/// <param name="pPartStartPointX">目标图片开始绘制处的坐标X值(通常为)</param>
        
/// <param name="pPartStartPointY">目标图片开始绘制处的坐标Y值(通常为)</param>
        
/// <param name="pPartWidth">目标图片的宽度</param>
        
/// <param name="pPartHeight">目标图片的高度</param>
        
/// <param name="pOrigStartPointX">原始图片开始截取处的坐标X值</param>
        
/// <param name="pOrigStartPointY">原始图片开始截取处的坐标Y值</param>
        
/// <param name="pFormat">保存格式,通常可以是jpeg</param>
        public void GetPart(string pPath, string pSavedPath, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY)
        {
            Image originalImg = Image.FromFile(pPath);

            Bitmap partImg = new Bitmap(pPartWidth, pPartHeight);
            Graphics graphics = Graphics.FromImage(partImg);
            Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight));//目标位置
            Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight));//原图位置(默认从原图中截取的图片大小等于目标图片的大小)

            graphics.DrawImage(originalImg, destRect, origRect, GraphicsUnit.Pixel);
            partImg.Save(pSavedPath, ImageFormat.Jpeg);
            partImg.Dispose();
        }
调用 GetPart(@"F:\我的图片\意大利道罗麦特山.jpg"@"F:\我的图片\意大利道罗麦特山"+DateTime.Now.ToString("yyyyMMddhhmmss")+".jpg",0,0,200,200,453,357);

 

posted @ 2012-07-24 14:56  青春岁月,无怨无悔  阅读(527)  评论(0编辑  收藏  举报