public BitmapPosData(BitmapPosData value) { region = new Region(value.region.GetRegionData()); bitmap = new Bitmap(value.bitmap); location = value.location; ptGripDelta = value.ptGripDelta; if (value.ptPolygon != null) { ptPolygon = (Point[])value.ptPolygon.Clone(); } isPicked = false; }
public static BitmapPosData CreateFromDesktopImageInClippedRegion(Region rg, Point[] ptPath) { Console.WriteLine("CopyDesktopImageToClippedRegion"); BitmapPosData data = null; int screenX; int screenY; IntPtr hBmp; IntPtr hdcScreen = GetDC(GetDesktopWindow()); IntPtr hdcCompatible = CreateCompatibleDC(hdcScreen); screenX = GetSystemMetrics(0); screenY = GetSystemMetrics(1); hBmp = CreateCompatibleBitmap(hdcScreen, screenX, screenY); if (hBmp != IntPtr.Zero) { IntPtr hOldBmp = (IntPtr)SelectObject(hdcCompatible, hBmp); BitBlt(hdcCompatible, 0, 0, screenX, screenY, hdcScreen, 0, 0, 13369376); SelectObject(hdcCompatible, hOldBmp); DeleteDC(hdcCompatible); ReleaseDC(GetDesktopWindow(), hdcScreen); Bitmap bmpDt = System.Drawing.Image.FromHbitmap(hBmp); Graphics gd = Graphics.FromImage(bmpDt); RectangleF rc = rg.GetBounds(gd); Bitmap bmpClip = new Bitmap((int)rc.Width, (int)rc.Height); Graphics gt = Graphics.FromImage(bmpClip); Region rgnTempPict = rg.Clone(); rgnTempPict.Translate(-rc.X, -rc.Y); gt.SetClip(rgnTempPict, System.Drawing.Drawing2D.CombineMode.Replace); RectangleF rc2 = rg.GetBounds(gt); gt.DrawImage(bmpDt, new Rectangle(0, 0, bmpClip.Width, bmpClip.Height), new Rectangle((int)rc.X, (int)rc.Y, (int)rc.Width, (int)rc.Height), GraphicsUnit.Pixel); for (int i = 0; i < ptPath.Length; i++) { ptPath[i].Offset((int)-rc.X, (int)-rc.Y); } data = new BitmapPosData(bmpClip, rgnTempPict.Clone(), new Point(0, 0), ptPath); rgnTempPict.Dispose(); gd.Dispose(); gt.Dispose(); DeleteObject(hBmp); GC.Collect(); } return data; }
public void CopyDesktopImageInClippedRegion(Region rg, Point[] ptPath) { Console.WriteLine("CopyDesktopImageToClippedRegion"); int screenX; int screenY; IntPtr hBmp; IntPtr hdcScreen = GetDC(GetDesktopWindow()); IntPtr hdcCompatible = CreateCompatibleDC(hdcScreen); screenX = GetSystemMetrics(0); screenY = GetSystemMetrics(1); hBmp = CreateCompatibleBitmap(hdcScreen, screenX, screenY); if (hBmp != IntPtr.Zero) { IntPtr hOldBmp = (IntPtr)SelectObject(hdcCompatible, hBmp); BitBlt(hdcCompatible, 0, 0, screenX, screenY, hdcScreen, 0, 0, 13369376); SelectObject(hdcCompatible, hOldBmp); DeleteDC(hdcCompatible); ReleaseDC(GetDesktopWindow(), hdcScreen); Bitmap bmpDt = System.Drawing.Image.FromHbitmap(hBmp); Graphics gd = Graphics.FromImage(bmpDt); RectangleF rc = rg.GetBounds(gd); Bitmap bmpClip = new Bitmap((int)rc.Width, (int)rc.Height); Graphics gt = Graphics.FromImage(bmpClip); Region rgnTempPict = rg.Clone(); rgnTempPict.Translate(-rc.X, -rc.Y); gt.SetClip(rgnTempPict, System.Drawing.Drawing2D.CombineMode.Replace); RectangleF rc2 = rg.GetBounds(gt); gt.DrawImage(bmpDt, new Rectangle(0, 0, bmpClip.Width, bmpClip.Height), new Rectangle((int)rc.X, (int)rc.Y, (int)rc.Width, (int)rc.Height), GraphicsUnit.Pixel); for (int i = 0; i < ptPath.Length; i++) { ptPath[i].Offset((int)-rc.X, (int)-rc.Y); } mCopiedBmpData = new FormNote.BitmapPosData(bmpClip, rgnTempPict.Clone(), new Point(0, 0), ptPath); /* //temp image for moving (transparent) if (mPickedBmpDataTmp != null) { mPickedBmpDataTmp.Dispose(); mPickedBmpDataTmp = null; } mPickedBmpDataTmp = new BitmapPosData(); mPickedBmpDataTmp.location = mPickedBmpData.location; mPickedBmpDataTmp.region = mPickedBmpData.region.Clone(); mPickedBmpDataTmp.bmp = new Bitmap(mPickedBmpData.bmp); for (int y = 0; y < mPickedBmpDataTmp.bmp.Height; y++) { for (int x = 0; x < mPickedBmpDataTmp.bmp.Width; x++) { Color c = mPickedBmpDataTmp.bmp.GetPixel(x, y); Color cc = Color.FromArgb(127, c.R, c.G, c.B); mPickedBmpDataTmp.bmp.SetPixel(x, y, cc); } } */ rgnTempPict.Dispose(); gd.Dispose(); gt.Dispose(); DeleteObject(hBmp); GC.Collect(); } }
public void SelPict() { Console.WriteLine("SelPict"); //this.Visible = false; foreach (Form c in this.MdiParent.MdiChildren) { c.Visible = false; } try { DialogResult res = System.Windows.Forms.DialogResult.Cancel; FormSelRegion frm = new FormSelRegion(); do { res = frm.ShowDialog(); Console.WriteLine("FormSelRegion.DialogResult is {0}", res.ToString()); } while (res == System.Windows.Forms.DialogResult.No); if (res == System.Windows.Forms.DialogResult.Yes) { Console.WriteLine("FormSelRegion.DialogResult==Yes"); if (mCopiedBmpData != null) { mCopiedBmpData.Dispose(); mCopiedBmpData = null; } System.Threading.Thread.Sleep(500); mCopiedBmpData = BitmapPosData.CreateFromDesktopImageInClippedRegion(frm.SelRegion, frm.RegionPath); mActiveNote.CopyPickedImage(mCopiedBmpData); mMode = MODE.IMG_PICK; mActiveNote.setInkMode(mMode); } foreach (Form c in this.MdiParent.MdiChildren) { c.Visible = true; if (c.GetType() == typeof(FormNote)) { FormNote note = (FormNote)c; note.Location = note.mPreviousLocation; } } this.Cursor = System.Windows.Forms.Cursors.Hand; mActiveNote.Activate(); this.Visible = true; } catch (Exception e) { Console.WriteLine(e.Message); } }
private void pictCopy_Click(object sender, EventArgs e) { if (mActiveNote == null) return; if (mActiveNote.IsImagePicked) { //DrawBmpToBg(mPickedBmpDataBackup, mBmpBG); if (mCopiedBmpData != null) { mCopiedBmpData.Dispose(); mCopiedBmpData = null; } mCopiedBmpData = new BitmapPosData(mActiveNote.mPickedBmpDataRef); pictPaste.Visible = true; } else { mActiveNote.CopyToClipboard(); mActiveNote.setInkMode(mMode); ActivatePaintingScreen(); } }
public void CopyPickedImage(BitmapPosData pickedData) { BitmapPosData dataNew = new BitmapPosData(pickedData); dataNew.location.X = 0; dataNew.location.Y = 0; dataNew.region.Translate(-pickedData.location.X, -pickedData.location.Y); mBgBitmaps.Add(dataNew); DrawBackGroundImageAndGrid(); }
void panel1_MouseDown(object sender, MouseEventArgs e) { Console.WriteLine("panel1_MouseDown"); bool needRedraw = false; if (e.Button == System.Windows.Forms.MouseButtons.Left) { if (mInkPicture.InkEnabled == false) { if (mPickedBmpDataRef != null) { mPickedBmpDataRef = null; } if (mPickedBmpDataTmp != null) { mPickedBmpDataTmp.Dispose(); mPickedBmpDataTmp = null; } // clear all picked flag. foreach (BitmapPosData bmpData in mBgBitmaps) { if (bmpData.isPicked) { bmpData.isPicked = false; needRedraw = true; } } for (int i = mBgBitmaps.Count - 1; i >= 0; i--) { BitmapPosData bmpData = mBgBitmaps[i]; if (bmpData.region.IsVisible(e.Location)) { mPickedBmpDataRef = bmpData; needRedraw = true; //mPickedBmpData.ptGripDelta = new Point(e.X - bmpData.location.X, e.Y - bmpData.location.Y); //mBgBitmaps.Remove(bmpData); bmpData.isPicked = true; mPickedBmpDataTmp = new BitmapPosData(bmpData); mPickedBmpDataTmp.ptGripDelta = new Point(e.X - bmpData.location.X, e.Y - bmpData.location.Y); //make transparent image for (int y = 0; y < mPickedBmpDataTmp.bitmap.Height; y++) { for (int x = 0; x < mPickedBmpDataTmp.bitmap.Width; x++) { Color c = mPickedBmpDataTmp.bitmap.GetPixel(x, y); Color cc = Color.FromArgb(127, c.R, c.G, c.B); mPickedBmpDataTmp.bitmap.SetPixel(x, y, cc); } } break; } } if (needRedraw) { //rebuild background image DrawBackGroundImageAndGrid(); if (IsImagePicked) { mFormPalette.OnImagePicked(); } } /* else if (mFormPalette.IsImageCopied) { if (mFormPalette.mCopiedBmpData != null) { mFormPalette.mCopiedBmpData.region.Translate( e.Location.X - (mFormPalette.mCopiedBmpData.location.X + mFormPalette.mCopiedBmpData.bitmap.Width / 2), e.Location.Y - (mFormPalette.mCopiedBmpData.location.Y + mFormPalette.mCopiedBmpData.bitmap.Height / 2)); mFormPalette.mCopiedBmpData.location.X = e.Location.X - mFormPalette.mCopiedBmpData.bitmap.Width / 2; mFormPalette.mCopiedBmpData.location.Y = e.Location.Y - mFormPalette.mCopiedBmpData.bitmap.Height / 2; DrawBmpToBg(mFormPalette.mCopiedBmpData, mBmpBG); this.mInkPicture.BackgroundImage = mBmpBG; mBgBitmaps.Add(mFormPalette.mCopiedBmpData); mFormPalette.mCopiedBmpData = null; mFormPalette.OnImagePickMode(); } } */ } } }
/* Size DrawBmpToBg(BitmapPosData bmpdt, Image imgBg) { Graphics g = Graphics.FromImage(imgBg); g.SetClip(bmpdt.region, System.Drawing.Drawing2D.CombineMode.Replace); g.DrawImage(bmpdt.bmp, bmpdt.location); g.Dispose(); Size reqSize = new Size(bmpdt.location.X + bmpdt.bmp.Width, bmpdt.location.Y + bmpdt.bmp.Height); if (reqSize.Height > mBmpSize.Height) mBmpSize.Height = reqSize.Height; if (reqSize.Width > mBmpSize.Width) mBmpSize.Width = reqSize.Width; return reqSize; } * */ Size DrawBmpToBg(BitmapPosData bmpdt, Image imgBg) { Graphics g = Graphics.FromImage(imgBg); g.SetClip(bmpdt.region, System.Drawing.Drawing2D.CombineMode.Replace); g.DrawImage(bmpdt.bitmap, bmpdt.location); Size reqSize = new Size(bmpdt.location.X + bmpdt.bitmap.Width, bmpdt.location.Y + bmpdt.bitmap.Height); if (reqSize.Height > mBmpSize.Height) mBmpSize.Height = reqSize.Height; if (reqSize.Width > mBmpSize.Width) mBmpSize.Width = reqSize.Width; if (bmpdt.isPicked) { g.ResetClip(); Color drawC = Color.FromArgb(255, 255 - mBgColor.R, 255 - mBgColor.G, 255 - mBgColor.B); Pen p = new Pen(drawC, 2); g.DrawPolygon(p, bmpdt.Points); } g.Dispose(); return reqSize; }
public void LoadData(string path) { Console.WriteLine("LoadData >>"); System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(path); XmlNodeList dataNodes = doc.GetElementsByTagName("data"); if (dataNodes.Count > 0) { XmlNode dataNode = dataNodes[0]; int x = 0; int y = 0; int w = 0; int h = 0; if (dataNode.Attributes["x"] != null && dataNode.Attributes["y"] != null) { x = Int32.Parse(dataNode.Attributes["x"].Value); y = Int32.Parse(dataNode.Attributes["y"].Value); } if (dataNode.Attributes["width"] != null && dataNode.Attributes["height"] != null) { w = Int32.Parse(dataNode.Attributes["width"].Value); h = Int32.Parse(dataNode.Attributes["height"].Value); } x = x < 0 ? 0 : x; x = (x + w) > Screen.PrimaryScreen.WorkingArea.Width ? Screen.PrimaryScreen.WorkingArea.Width - w : x; y = y < 0 ? 0 : y; y = (y + h) > Screen.PrimaryScreen.WorkingArea.Height ? Screen.PrimaryScreen.WorkingArea.Height - h : y; this.Location = new Point(x, y); this.Size = new Size(w, h); if (dataNode.Attributes["bgcolor"] != null) { try { this.mBgColor = Color.FromArgb(Int32.Parse(dataNode.Attributes["bgcolor"].Value)); } catch (Exception e) { this.mBgColor = Color.Wheat; } } } XmlNodeList strokeNodes = doc.GetElementsByTagName("stroke"); if (strokeNodes.Count > 0) { XmlNode strokeNode = strokeNodes[0]; XmlCDataSection cdata_stroke = (XmlCDataSection)strokeNode.FirstChild; byte[] decoded = System.Convert.FromBase64String(cdata_stroke.Data); mInkPicture.Ink.Load(decoded); mInkPicture.Refresh(); } XmlNodeList imageNodes = doc.GetElementsByTagName("image"); foreach(XmlNode imageNode in imageNodes) { Bitmap bmp = null; Region rgn = null; Point loc = new Point(); Point[] ptPolygon = null; loc.X = Int32.Parse(imageNode.Attributes["x"].Value); loc.Y = Int32.Parse(imageNode.Attributes["y"].Value); foreach(XmlNode node in imageNode.ChildNodes) { if (node.Name == "region") { XmlCDataSection cdata_region = (XmlCDataSection)node.FirstChild; byte[] decoded = System.Convert.FromBase64String(cdata_region.Data); Region regionTmp = new Region(); System.Drawing.Drawing2D.RegionData region2Data = regionTmp.GetRegionData(); region2Data.Data = decoded; rgn = new Region(region2Data); regionTmp.Dispose(); } else if (node.Name == "polygon") { string[] xdata = null; string[] ydata = null; foreach (XmlNode c in node.ChildNodes) { if(c.Name == "xdata") { xdata = c.InnerText.Split(','); } else if (c.Name == "ydata") { ydata = c.InnerText.Split(','); } } if (xdata != null && ydata != null) { int count = xdata.Length; ptPolygon = new Point[count]; for (int i = 0; i < count; i++) { int x = Convert.ToInt32(xdata[i]); int y = Convert.ToInt32(ydata[i]); ptPolygon[i] = new Point(x, y); } } } else if (node.Name == "bmpdata") { XmlCDataSection cdata_bmp = (XmlCDataSection)node.FirstChild; byte[] decoded = System.Convert.FromBase64String(cdata_bmp.Data); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { ms.Write(decoded, 0, decoded.Length); Bitmap bmpTmp = (Bitmap)Bitmap.FromStream(ms, false, true); // Color format of a bitmap loaded from stream is 32rgb, // but default bitmap format is 32Argb // it cause a problem when save this bitmap. // so, create new bitmap and copy its color one by one bmp = new Bitmap(bmpTmp.Width, bmpTmp.Height); for (int x = 0; x < bmpTmp.Width; x++) { for (int y = 0; y < bmpTmp.Height; y++) { Color c = bmpTmp.GetPixel(x, y); bmp.SetPixel(x, y, c); } } } /* System.IO.FileStream fsw = System.IO.File.OpenWrite("temp.bmp"); int len = decoded.Length; fsw.Write(decoded, 0, len); fsw.Close(); System.IO.FileStream fsr = System.IO.File.OpenRead("temp.bmp"); bmpdt.bmp = (Bitmap)Image.FromStream(fsr); fsr.Close(); */ } } BitmapPosData bmpdt = new BitmapPosData(bmp, rgn, loc, ptPolygon); mBgBitmaps.Add(bmpdt); DrawBmpToBg(bmpdt, mBmpBG); } Console.WriteLine("<< LoadData"); }
/* public void DeleteSelectedStrokes() { if (mInkPicture.EditingMode == InkOverlayEditingMode.Select) { mInkPicture.Ink.DeleteStrokes(mInkPicture.Selection); } } */ public void DeletePickedImage() { if (IsImagePicked) { //dispose selected image if (mPickedBmpDataRef != null) { foreach(BitmapPosData data in mBgBitmaps) { if (data.bitmap == mPickedBmpDataRef.bitmap && data.location == mPickedBmpDataRef.location && data.region == mPickedBmpDataRef.region) { bool bRet = mBgBitmaps.Remove(data); mPickedBmpDataRef = null; mFormPalette.OnImagePickMode(); break; } } } //rebuild background image DrawBackGroundImageAndGrid(); } }