public static void Draw(DrawSettings setting) { settings = setting; if (!File.Exists(settings.ImagePath)) { System.Windows.Forms.MessageBox.Show("找不到图片啦~"); settings.Finished(0, 0); return; } // 读取 cookie //Console.WriteLine("开始初始化Cookie..."); var cookies = new CookieContainer(); var eachCookie = settings.Cookie.Split(';'); foreach (string cookie in eachCookie) { var tmpCookie = cookie.Replace(" ", ""); var cookieName = tmpCookie.Split('=')[0]; var cookieValue = tmpCookie.Split('=')[1].Replace(",", "%2C"); cookies.Add(new Cookie(cookieName, cookieValue, "/", @".bilibili.com")); //Console.WriteLine("正在添加Cookie: {0}", cookieName); } // 载入图片 var bitmap = new Bitmap(settings.ImagePath); int x = settings.ImageStartX, y = settings.ImageStartY, i = 0; //Console.WriteLine("开始创作~"); settings.Started(); for (; y < bitmap.Height; y++) { for (; x < bitmap.Width; x++) { var color = bitmap.GetPixel(x, y); var colorFlag = palettle.ConvertToFlag(color); if (colorFlag == "") { continue; } i++; var respone = SendDataByPost(DRAWBOARD_ACTION_DRAW_URL, GetPostData(x, y, colorFlag), ref cookies); var waitTime = Regex.Match(respone, @"(?<=\{""time"":)\d+(?=\})").Value; if (respone.Contains(@"""code"":-400")) { settings.DrawPixelCallback(false, false, "需要等待" + waitTime + "秒\n", i); } //Console.WriteLine("第{0}次绘画失败, 需要等待{1}秒~ x = {2},y = {3}", i, waitTime, x, y); else if (respone.Contains(@"""code"":0")) { settings.DrawPixelCallback(true, false, string.Format("位置({0}, {1}), 开始等待180秒.\n", new object[] { x, y }), i); } //Console.WriteLine("第{0}次绘画成功, 开始等待{1}秒~ x = {2},y = {3}", i, waitTime, x, y); else if (respone.Contains(@"""code"":-101")) { settings.DrawPixelCallback(false, true, "未登录或未绑定手机.\n", i); return; } else { settings.DrawPixelCallback(false, true, respone, i); } Thread.Sleep(Int32.Parse(waitTime) * 1000); } x = 0; } settings.Finished(x, y); }
public async static void DrawAsync(DrawSettings setting) { await Task.Run(() => Draw(setting)); }