public unsafe SharpDX.WIC.Bitmap CreateWICBitmapFromGDI(System.Drawing.Bitmap gdiBitmap) { var wicFactory = new ImagingFactory(); var wicBitmap = new SharpDX.WIC.Bitmap(wicFactory, gdiBitmap.Width, gdiBitmap.Height, PixelFormat.Format32bppBGRA, BitmapCreateCacheOption.CacheOnLoad); System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, gdiBitmap.Width, gdiBitmap.Height); var btmpData = gdiBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); byte *pGDIData = (byte *)btmpData.Scan0; using (BitmapLock bl = wicBitmap.Lock(BitmapLockFlags.Write)) { byte *pWICData = (byte *)bl.Data.DataPointer; for (int y = 0; y < gdiBitmap.Height; y++) { int offsetWIC = y * bl.Stride; int offsetGDI = y * btmpData.Stride; for (int x = 0; x < gdiBitmap.Width; x++) { pWICData[offsetWIC + 0] = pGDIData[offsetGDI + 0]; pWICData[offsetWIC + 1] = pGDIData[offsetGDI + 1]; pWICData[offsetWIC + 2] = pGDIData[offsetGDI + 2]; pWICData[offsetWIC + 3] = pGDIData[offsetGDI + 3]; offsetWIC += 4; offsetGDI += 4; } } } gdiBitmap.UnlockBits(btmpData); return(wicBitmap); }
private void Save_1_event_Click(object sender, RoutedEventArgs e) { bool en_saved = true; //是否已经存档? SaveData.SaveInfo?imp = null; //如果有存档,这是此次存档的信息 try { imp = SaveData.Save1; } catch { en_saved = false; } if (en_saved)//如果有存档 { var pos = Mouse.GetPosition(save_1_event); if (pos.X < (save_1_event.Width / 2.0)) { imp.Value.imp.Dispose(); if (disableSave) { return; } var data_sp = _last_draw.Lock(BitmapLockFlags.Read); var m_img = new Bitmap(_last_draw.Size.Width, _last_draw.Size.Height, data_sp.Stride, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, data_sp.Data.DataPointer); save_1_event.Background = new ImageBrush(BitmapToBitmapSource(m_img)); save_1_content.Text = "第" + chapter.ToString() + "第" + chapter.ToString() + "章 " + "第" + _frame.ToString() + "节" + "\n" + DateTime.Now.ToLocalTime().ToString(); while (true) { try { File.Delete(@"data/save001.png"); break; } catch { Thread.Sleep(100); continue; } } SaveData.Save1 = new SaveData.SaveInfo() { chapter = this.chapter, frames = _frame, imp = m_img, comment = save_1_content.Text }; data_sp.Dispose(); m_img.Dispose(); return; }//如果点击按钮的左边,则覆盖存档 if (alreadyKeep) { return; } alreadyKeep = true; //禁止快速重复点击 if (MainWindow.TheatreMode == null) //如果是从标题进入读档 { EasyAmal _mpos2 = new EasyAmal(this.Forgan, "(Opacity)", 1.0, 0.0, SharedSetting.SwitchSpeed, (e, c) => { MainWindow.TheatreMode = MainWindow.SwitchToSignalTheatre(imp.Value.chapter, imp.Value.frames, null); MainWindow.Sldata = null; }); _mpos2.Start(true); return; } MainWindow.TheatreMode.Dispose(); EasyAmal mpos2 = new EasyAmal(this.Forgan, "(Opacity)", 1.0, 0.0, SharedSetting.SwitchSpeed, (e, c) => { MainWindow.TheatreMode = MainWindow.SwitchToSignalTheatre(imp.Value.chapter, imp.Value.frames, null); MainWindow.Sldata = null; }); mpos2.Start(true); } else { if (disableSave) { return; } var data_sp = _last_draw.Lock(BitmapLockFlags.Read); var m_img = new Bitmap(_last_draw.Size.Width, _last_draw.Size.Height, data_sp.Stride, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, data_sp.Data.DataPointer); save_1_event.Background = new ImageBrush(BitmapToBitmapSource(m_img)); save_1_content.Text = "第" + chapter.ToString() + "章 " + "第" + _frame.ToString() + "节" + "\n" + DateTime.Now.ToLocalTime().ToString();; SaveData.Save1 = new SaveData.SaveInfo() { chapter = this.chapter, frames = _frame, imp = m_img, comment = save_1_content.Text }; data_sp.Dispose(); m_img.Dispose(); } }
/// <inheritdoc /> /// <summary> /// Creates a single bitmap from the captured frames and returns an object with its information /// </summary> /// <returns>A <see cref="BitmapData" /> containing raw bitmap information</returns> public override BitmapData LockFrameBitmap() { using (var factory = new ImagingFactory2()) { var bmp = new Bitmap(factory, CaptureBounds.Width, CaptureBounds.Height, PixelFormat.Format32bppBGRA, BitmapCreateCacheOption.CacheOnDemand); // caller is responsible for disposing BitmapLock BitmapLock data = bmp.Lock(BitmapLockFlags.Write); int minX = this.rects.Select(b => b.X).Min(); int minY = this.rects.Select(b => b.Y).Min(); // map textures for (int i = 0; i < Surfaces.Length; i++) { DataRectangle map = Surfaces[i].LockRectangle(LockFlags.ReadOnly); IntPtr dstScan0 = data.Data.DataPointer, srcScan0 = map.DataPointer; int dstStride = data.Stride, srcStride = map.Pitch; int srcWidth = this.regions[i].Width, srcHeight = this.regions[i].Height; int dstPixelSize = dstStride / data.Size.Width, srcPixelSize = srcStride / srcWidth; int dstX = this.rects[i].X - minX, dstY = this.rects[i].Y - minY; for (int y = 0; y < srcHeight; y++) { Utilities.CopyMemory(IntPtr.Add(dstScan0, dstPixelSize * dstX + (y + dstY) * dstStride), IntPtr.Add(srcScan0, y * srcStride), srcPixelSize * srcWidth); } Surfaces[i].UnlockRectangle(); } return(new BitmapData { Width = CaptureBounds.Width, Height = CaptureBounds.Height, PixelFormat = bmp.PixelFormat, Scan0 = data.Data.DataPointer, Stride = data.Stride, LockPointer = data.NativePointer }); } }