protected override void OnRender(DrawingContext dc) { var bitmap = new WriteableBitmap((int)(Screen.PixelWidth * this.Zoom), (int)(Screen.PixelHeight * this.Zoom), 96, 96, PixelFormats.Pbgra32, null); base.OnRender(dc); var size = Screen.Tileset.TileSize * this.Zoom; for (int y = 0; y < Screen.Height; y++) { for (int x = 0; x < Screen.Width; x++) { var tile = Screen.TileAt(x, y); var location = tile.Sprite.CurrentFrame.SheetLocation; var rect = new Rect(0, 0, location.Width, location.Height); if (_grayscale) { var image = SpriteBitmapCache.GetOrLoadFrameGrayscale(Screen.Tileset.SheetPath.Absolute, location); bitmap.Blit(new Rect(x * size, y * size, size, size), image, rect); } else { var image = SpriteBitmapCache.GetOrLoadFrame(Screen.Tileset.SheetPath.Absolute, location); bitmap.Blit(new Rect(x * size, y * size, size, size), image, rect); } } } dc.DrawImage(bitmap, new Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight)); }
/// <summary> /// 残件:Typeface 型をもらって TextBlock に反映するようにしたほうが良い /// </summary> /// <param name="bmp"></param> /// <param name="text"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="orientation"></param> public static void DrawString(this WriteableBitmap bmp, string text, double x, double y, Orientation orientation = Orientation.Horizontal) { var textBmp = GetTextWriteableBitmap(text, orientation); bmp.Blit(new Point(x, y), textBmp, new Rect(0, 0, textBmp.PixelWidth, textBmp.PixelHeight), Colors.White, WriteableBitmapExtensions.BlendMode.Alpha); }
public static WriteableBitmap MakeSmallerCopy(this WriteableBitmap original, int maxDimension) { var ratio = 0.0; if (original.PixelHeight > original.PixelWidth) { ratio = ((double)original.PixelHeight) / maxDimension; } else { ratio = ((double)original.PixelWidth) / maxDimension; } if (ratio > 1.0) ratio = 1.0; var newWidth = (int)Math.Floor(((double)original.PixelWidth) / ratio); var newHeight = (int)Math.Floor(((double)original.PixelHeight) / ratio); var toReturn = new WriteableBitmap(newWidth, newHeight); toReturn.Blit(toReturn.BoundingRect(), original, original.BoundingRect()); return toReturn; }
/// <summary> /// Generates the guild emblem image. /// </summary> /// <param name="guildEmblemData">The guild emblem data.</param> /// <param name="guildSide">The guild side.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <returns></returns> public static BitmapSource GenerateGuildEmblemImage(GuildEmblem guildEmblemData, GuildSide guildSide, int width = 216, int height = 240) { WriteableBitmap guildEmblemImage; try { guildEmblemImage = new WriteableBitmap(216, 240); var factionBackgroundImage = new WriteableBitmap(216, 216).FromResource(String.Format("Images/GuildDetails/Emblem_Background_{0}.png", guildSide)); guildEmblemImage.Blit(new Rect(0, 0, 216, 216), factionBackgroundImage, new Rect(0, 0, 216, 216), WriteableBitmapExtensions.BlendMode.Alpha); var shadowImage = new WriteableBitmap(179, 210).FromResource("Images/GuildDetails/Emblem_Shadow.png"); guildEmblemImage.Blit(new Rect(18, 27, 179, 210), shadowImage, new Rect(0, 0, 179, 210), WriteableBitmapExtensions.BlendMode.Alpha); var backgroundColor = GetColorFromHexString(guildEmblemData.BackgroundColor); var emblemBackgroundImage = new WriteableBitmap(179, 210).FromResource("Images/GuildDetails/Emblem_Background.png"); guildEmblemImage.Blit(new Rect(18, 27, 179, 210), emblemBackgroundImage, new Rect(0, 0, 179, 210), backgroundColor, WriteableBitmapExtensions.BlendMode.Alpha); var borderColor = GetColorFromHexString(guildEmblemData.BorderColor); var borderImage = new WriteableBitmap(147, 159).FromResource(String.Format("Images/GuildDetails/Emblem_Border_{0}.png", guildEmblemData.Border)); guildEmblemImage.Blit(new Rect(31, 40, 147, 159), borderImage, new Rect(0, 0, 147, 159), borderColor, WriteableBitmapExtensions.BlendMode.Alpha); var iconColor = GetColorFromHexString(guildEmblemData.IconColor); var iconImage = new WriteableBitmap(125, 125).FromResource(String.Format("Images/GuildDetails/Emblems/Emblem_{0}.png", guildEmblemData.Icon.ToString().PadLeft(3, '0'))); guildEmblemImage.Blit(new Rect(33, 57, 125, 125), iconImage, new Rect(0, 0, 125, 125), iconColor, WriteableBitmapExtensions.BlendMode.Alpha); var hooksImage = new WriteableBitmap(179, 32).FromResource("Images/GuildDetails/Emblem_Hooks.png"); guildEmblemImage.Blit(new Rect(18, 27, 179, 32), hooksImage, new Rect(0, 0, 179, 32), WriteableBitmapExtensions.BlendMode.Alpha); } catch (Exception ex) { guildEmblemImage = new WriteableBitmap(216, 240).FromResource(String.Format("Images/GuildDetails/Emblem_Default_{0}.png", guildSide)); } var resized = guildEmblemImage.Resize(width, height, WriteableBitmapExtensions.Interpolation.Bilinear); return resized; }
private void ExtractImage(List<WriteableBitmap> tempTiles, BitmapSource image, int spacing, int offset, IProgress<ProgressDialogState> progress = null) { var sourceImage = BitmapFactory.ConvertToPbgra32Format(image); var jump = 16 + spacing; var totalTiles = ((image.PixelWidth - offset) / jump) * ((image.PixelHeight - offset) / jump); var currentTile = 0; for (var y = offset; y < image.PixelHeight; y += jump) { for (var x = offset; x < image.PixelWidth; x += jump) { var tileImage = new WriteableBitmap(16, 16, 96, 96, PixelFormats.Pbgra32, null); tileImage.Blit(new System.Windows.Rect(0, 0, 16, 16), sourceImage, new System.Windows.Rect(x, y, 16, 16)); tempTiles.Add(tileImage); currentTile++; if (progress != null) { progress.Report(new ProgressDialogState() { ProgressPercentage = currentTile * 100 / totalTiles, Description = string.Format("Extracting {0} / {1}", currentTile, totalTiles) }); } } } }
private void AppendNewTilesToSheet(List<WriteableBitmap> tempTiles) { var total = tempTiles.Count; var tileWidth = Tilesheet.PixelWidth / 16; var addedTileHeight = (int)Math.Ceiling(total / (double)tileWidth); var tilesheet = new WriteableBitmap(Tilesheet.PixelWidth, Tilesheet.PixelHeight + addedTileHeight * 16, 96, 96, PixelFormats.Pbgra32, null); var writeableSource = BitmapFactory.ConvertToPbgra32Format(Tilesheet); var originalRect = new System.Windows.Rect(0, 0, Tilesheet.PixelWidth, Tilesheet.PixelHeight); tilesheet.Blit(originalRect, writeableSource, originalRect); var x = 0; var y = Tilesheet.PixelHeight; var source = new System.Windows.Rect(0, 0, 16, 16); foreach (var frame in tempTiles) { var dest = new System.Windows.Rect(x, y, 16, 16); tilesheet.Blit(dest, frame, source); var tile = Tileset.AddTile(); tile.Sprite.CurrentFrame.SetSheetPosition(x, y); if (x < 16 * (tileWidth - 1)) { x += 16; } else { x = 0; y += 16; } } Tilesheet = tilesheet; SpriteBitmapCache.InsertSource(Tileset.SheetPath.Absolute, tilesheet); Tileset.RefreshSheet(); }
public void CompactTilesheet() { var allFrames = RipAllFrames(); var total = allFrames.Count; var root = Math.Sqrt(total); var width = (int)Math.Ceiling(root); var height = (int)Math.Ceiling(total / (double)width); var tilesheet = new WriteableBitmap(width * 16, height * 16, 96, 96, PixelFormats.Pbgra32, null); var x = 0; var y = 0; var source = new System.Windows.Rect(0, 0, 16, 16); foreach (var frame in allFrames) { var dest = new System.Windows.Rect(x, y, 16, 16); tilesheet.Blit(dest, frame.Key, source); foreach (var spriteFrame in frame.Value) { spriteFrame.SetSheetPosition(x, y); } if (x < 16 * (width - 1)) { x += 16; } else { x = 0; y += 16; } } Tilesheet = tilesheet; SpriteBitmapCache.InsertSource(Tileset.SheetPath.Absolute, tilesheet); Tileset.RefreshSheet(); }
/** * Extracts image pixels into byte array "pixels" */ protected void GetImagePixels() { int w = image.PixelWidth; int h = image.PixelHeight; // int type = image.GetType().; if ((w != width) || (h != height) ) { // create new image with right size/format WriteableBitmap temp = new WriteableBitmap(width, height); temp.Blit(new Rect(0.0, 0.0, width, height), image, new Rect(0.0, 0.0, image.PixelWidth, image.PixelHeight)); image = temp; } /* ToDo: improve performance: use unsafe code */ pixels = new Byte[3 * image.PixelWidth * image.PixelHeight]; int count = 0; WriteableBitmap tempBitmap = new WriteableBitmap(image); for (int th = 0; th < image.PixelHeight; th++) { for (int tw = 0; tw < image.PixelWidth; tw++) { Color color = tempBitmap.GetPixel(tw, th); pixels[count] = color.R; count++; pixels[count] = color.G; count++; pixels[count] = color.B; count++; } } // pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); }
private WriteableBitmap CutFrame(WriteableBitmap sheet, Common.Geometry.Rectangle frameRect) { var wb = new WriteableBitmap(frameRect.Width, frameRect.Height, 96, 96, PixelFormats.Pbgra32, null); var srcRect = new System.Windows.Rect(frameRect.X, frameRect.Y, frameRect.Width, frameRect.Height); var destRect = new System.Windows.Rect(0, 0, frameRect.Width, frameRect.Height); wb.Blit(destRect, sheet, srcRect); return wb; }
private void btnSave_Click(object sender, RoutedEventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "JPEGファイル (*.jpg)|*.jpg|PNGファイル (*.png)|*.png|ビットマップファイル (*.bmp)|*.bmp"; if (sfd.ShowDialog() == true) { IImageEncoder enc; string fn = sfd.SafeFileName.ToLower(); if(fn.EndsWith(".jpg")) { enc = new JpegEncoder(); } else if (fn.EndsWith(".png")) { enc = new PngEncoder(); } else if (fn.EndsWith(".bmp")) { enc = new BmpEncoder(); } else { MessageBox.Show("エラー"); return; } double scaledWidth = (cropImage.Width * zoom); double scaledHeight = (cropImage.Height * zoom); double scaledLeft = (imageWidth - scaledWidth) / 2; double scaledTop = (imageHeight - scaledHeight) / 2; Rect r = new Rect((double)cropCanvas.GetValue(Canvas.LeftProperty), (double)cropCanvas.GetValue(Canvas.TopProperty), cropCanvas.Width, cropCanvas.Height); Rect r2 = new Rect(scaledLeft, scaledTop, scaledWidth , scaledHeight); r.Intersect(r2); double clipedLeft = r.Left - scaledLeft; double clipedTop = r.Top - scaledTop; r.X = clipedLeft; r.Y = clipedTop; ScaleTransform st = new ScaleTransform(); st.ScaleX = zoom; st.ScaleY = zoom; WriteableBitmap wb = new WriteableBitmap(cropImage, st); WriteableBitmap outputWb = new WriteableBitmap((int)r.Width, (int)r.Height); outputWb.Blit(new Rect(0, 0, r.Width, r.Height), wb, new Rect(r.Left, r.Top, r.Width, r.Height)); ImageTools.Image img = ToImage(outputWb); using (Stream stream = sfd.OpenFile()) { enc.Encode(img, stream); } } }
public static string GenereateMosaicPicture(IList<BitmapImage> images, string filename) { string key = string.Empty; try { // Generate avatar in bitmap. var bmp = new WriteableBitmap(PICTURE_DIMENSION, PICTURE_DIMENSION); bmp.Clear(); int x = 0; int y = 0; int w = 0; int h = 0; if (images.Count == 1) { w = PICTURE_DIMENSION; h = PICTURE_DIMENSION; } else if (images.Count == 2) { w = PART_DIMENSION; h = PICTURE_DIMENSION; } else { w = PART_DIMENSION; h = PART_DIMENSION; } for (int i = 0; i < images.Count && i < 4; ) { if (images[i] == null) continue; var wb = new WriteableBitmap(images[i]); if (wb.PixelHeight < h || wb.PixelWidth < w) { wb = wb.Resize(PICTURE_DIMENSION, PICTURE_DIMENSION, WriteableBitmapExtensions.Interpolation.Bilinear); } i++; bmp.Blit(new Rect(x, y, w, h), wb, new Rect(0, 0, w, h)); if (w == PART_DIMENSION) x += w + 2; // Handle empty space between photos else x += w; if (x >= PICTURE_DIMENSION) { x = 0; if (h == PART_DIMENSION) y += h + 2; // Handle empty space between photos else y += h; } if (y >= PICTURE_DIMENSION) break; } if (_SaveJpeg(bmp, filename)) key = filename; } catch (Exception ex) { Debug.WriteLine("GenereateMosaicPicture failed." + ex.Message); } return key; }
private void DrawGrid(WriteableBitmap bitmap) { if (CentralPoint.X == 0 && CentralPoint.Y == 0) { CentralPoint = new System.Windows.Point(bitmap.PixelWidth / 2, bitmap.PixelHeight / 2); } WriteableBitmap tempbitmap = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight, bitmap.DpiX, bitmap.DpiY, PixelFormats.Pbgra32, bitmap.Palette); tempbitmap.DrawLine(0, (int)CentralPoint.Y - (StarWindowSize / 2), bitmap.PixelWidth, (int)CentralPoint.Y - (StarWindowSize / 2), Colors.White); tempbitmap.DrawLine(0, (int)CentralPoint.Y + (StarWindowSize / 2), bitmap.PixelWidth, (int)CentralPoint.Y + (StarWindowSize / 2), Colors.White); tempbitmap.DrawLine((int)CentralPoint.X - (StarWindowSize / 2), 0, (int)CentralPoint.X - (StarWindowSize / 2), bitmap.PixelHeight, Colors.White); tempbitmap.DrawLine((int)CentralPoint.X + (StarWindowSize / 2), 0, (int)CentralPoint.X + (StarWindowSize / 2), bitmap.PixelHeight, Colors.White); bitmap.Blit(new Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), tempbitmap, new Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight)); }
private async Task<WriteableBitmap> Screen() { //截图 await Task.Delay(200); using (System.IO.Stream stream1 = Application.GetResourceStream(new Uri(@"Assets/qcode.png", UriKind.Relative)).Stream) { double width = Application.Current.Host.Content.ActualWidth; double heigth = Application.Current.Host.Content.ActualHeight; WriteableBitmap wbmp = new WriteableBitmap((int)width, (int)heigth); wbmp.Render(App.Current.RootVisual, null); wbmp.Invalidate(); WriteableBitmap wideBitmap = new WriteableBitmap((int)width, (int)(heigth + 280)); BitmapImage image1 = new BitmapImage(); image1.SetSource(stream1); stream1.Close(); //stream1.Flush(); wideBitmap.Blit(new Rect(0, 0, width, heigth), new WriteableBitmap(wbmp), new Rect(0, 0, wbmp.PixelWidth, wbmp.PixelHeight), WriteableBitmapExtensions.BlendMode.Additive); wideBitmap.Blit(new Rect((width - 280) / 2, heigth, 280, 280), new WriteableBitmap(image1), new Rect(0, 0, image1.PixelWidth, image1.PixelHeight), WriteableBitmapExtensions.BlendMode.Additive); return wideBitmap; } }
public void Init(Syscalls syscalls, Core core, Runtime runtime) { PhoneApplicationFrame frame = (PhoneApplicationFrame)Application.Current.RootVisual; double screenWidth = System.Windows.Application.Current.Host.Content.ActualWidth; double screenHeight = System.Windows.Application.Current.Host.Content.ActualHeight; if ((int)screenHeight == 0) throw new Exception("screenHeight"); PhoneApplicationPage mainPage = (PhoneApplicationPage)frame.Content; mMainImage = new Image(); mainPage.Width = screenWidth; mainPage.Height = screenHeight; mMainImage.Width = screenWidth; mMainImage.Height = screenHeight; mainPage.Content = mMainImage; mClipRect.X = 0.0; mClipRect.Y = 0.0; mClipRect.Width = screenWidth; mClipRect.Height = screenHeight; // no apparent effect on memory leaks. runtime.RegisterCleaner(delegate() { MoSync.Util.RunActionOnMainThreadSync(() => { mainPage.Content = null; }); }); mBackBuffer = new WriteableBitmap( (int)screenWidth, (int)screenHeight); mFrontBuffer = new WriteableBitmap( (int)screenWidth, (int)screenHeight); mMainImage.Source = mFrontBuffer; // clear front and backbuffer. for (int i = 0; i < mFrontBuffer.PixelWidth * mFrontBuffer.PixelHeight; i++) { mBackBuffer.Pixels[i] = mBackBuffer.Pixels[i] = (int)(0xff<<24); } mCurrentDrawTarget = mBackBuffer; mCurrentWindowsColor = System.Windows.Media.Color.FromArgb(0xff, (byte)(mCurrentColor >> 16), (byte)(mCurrentColor >> 8), (byte)(mCurrentColor)); syscalls.maSetColor = delegate(int rgb) { int oldColor = (int)mCurrentColor; mCurrentColor = 0xff000000 | (uint)(rgb & 0xffffff); mCurrentWindowsColor = System.Windows.Media.Color.FromArgb(0xff, (byte)(mCurrentColor >> 16), (byte)(mCurrentColor >> 8), (byte)(mCurrentColor)); return oldColor & 0xffffff; }; syscalls.maSetClipRect = delegate(int x, int y, int w, int h) { MoSync.GraphicsUtil.ClipRectangle( x, y, w, h, 0, 0, mCurrentDrawTarget.PixelWidth, mCurrentDrawTarget.PixelHeight, out x, out y, out w, out h); mClipRect.X = x; mClipRect.Y = y; mClipRect.Width = w; mClipRect.Height = h; }; syscalls.maGetClipRect = delegate(int cliprect) { Memory mem = core.GetDataMemory(); mem.WriteInt32(cliprect + MoSync.Struct.MARect.left, (int)mClipRect.X); mem.WriteInt32(cliprect + MoSync.Struct.MARect.top, (int)mClipRect.Y); mem.WriteInt32(cliprect + MoSync.Struct.MARect.width, (int)mClipRect.Width); mem.WriteInt32(cliprect + MoSync.Struct.MARect.height, (int)mClipRect.Height); }; syscalls.maPlot = delegate(int x, int y) { mCurrentDrawTarget.SetPixel(x, y, (int)mCurrentColor); }; syscalls.maUpdateScreen = delegate() { //System.Array.Copy(mBackBuffer.Pixels, mFrontBuffer.Pixels, mFrontBuffer.PixelWidth * mFrontBuffer.PixelHeight); System.Buffer.BlockCopy(mBackBuffer.Pixels, 0, mFrontBuffer.Pixels, 0, mFrontBuffer.PixelWidth * mFrontBuffer.PixelHeight * 4); InvalidateWriteableBitmapOnMainThread(mFrontBuffer); }; syscalls.maFillRect = delegate(int x, int y, int w, int h) { // this function has a bug (it only fills one pixel less than the image.) //mCurrentDrawTarget.FillRectangle(x, y, x + w, y + h, (int)mCurrentColor); MoSync.GraphicsUtil.ClipRectangle( x, y, w, h, 0, 0, mCurrentDrawTarget.PixelWidth, mCurrentDrawTarget.PixelHeight, out x, out y, out w, out h); MoSync.GraphicsUtil.ClipRectangle( x, y, w, h, (int)mClipRect.X, (int)mClipRect.Y, (int)mClipRect.Width, (int)mClipRect.Height, out x, out y, out w, out h); if (w <= 0 || h <= 0) return; int index = x + y * mCurrentDrawTarget.PixelWidth; while(h-- != 0) { int width = w; while(width-- != 0) { mCurrentDrawTarget.Pixels[index] = (int)mCurrentColor; index++; } index += -w + mCurrentDrawTarget.PixelWidth; } }; syscalls.maLine = delegate(int x1, int y1, int x2, int y2) { GraphicsUtil.Point p1 = new GraphicsUtil.Point(x1, y1); GraphicsUtil.Point p2 = new GraphicsUtil.Point(x2, y2); if(!GraphicsUtil.ClipLine(p1, p2, (int)mClipRect.X, (int)(mClipRect.X+mClipRect.Width), (int)mClipRect.Y, (int)(mClipRect.Y+mClipRect.Height))) return; mCurrentDrawTarget.DrawLine((int)p1.x, (int)p1.y, (int)p2.x, (int)p2.y, (int)mCurrentColor); }; textBlock.FontSize = mCurrentFontSize; syscalls.maDrawText = delegate(int left, int top, int str) { String text = core.GetDataMemory().ReadStringAtAddress(str); if (text.Length == 0) return; DrawText(text, left, top); }; syscalls.maGetTextSize = delegate(int str) { String text = core.GetDataMemory().ReadStringAtAddress(str); int textWidth = 0; int textHeight = 0; GetTextSize(text, out textWidth, out textHeight); return MoSync.Util.CreateExtent(textWidth, textHeight); }; syscalls.maDrawTextW = delegate(int left, int top, int str) { String text = core.GetDataMemory().ReadWStringAtAddress(str); if (text.Length == 0) return; DrawText(text, left, top); }; syscalls.maGetTextSizeW = delegate(int str) { String text = core.GetDataMemory().ReadWStringAtAddress(str); int textWidth = 0; int textHeight = 0; GetTextSize(text, out textWidth, out textHeight); return MoSync.Util.CreateExtent(textWidth, textHeight); }; syscalls.maFillTriangleFan = delegate(int points, int count) { int[] newPoints = new int[count * 2 + 2]; for (int i = 0; i < count; i++) { newPoints[i * 2 + 0] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.x); newPoints[i * 2 + 1] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.y); } newPoints[count * 2 + 0] = core.GetDataMemory().ReadInt32(points + MoSync.Struct.MAPoint2d.x); newPoints[count * 2 + 1] = core.GetDataMemory().ReadInt32(points + MoSync.Struct.MAPoint2d.y); mCurrentDrawTarget.FillPolygon(newPoints, (int)mCurrentColor); }; syscalls.maFillTriangleStrip = delegate(int points, int count) { int[] xcoords = new int[count]; int[] ycoords = new int[count]; for (int i = 0; i < count; i++) { xcoords[i] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.x); ycoords[i] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.y); } for (int i = 2; i < count; i++) { mCurrentDrawTarget.FillTriangle( xcoords[i - 2], ycoords[i - 2], xcoords[i - 1], ycoords[i - 1], xcoords[i - 0], ycoords[i - 0], (int)mCurrentColor); } }; syscalls.maSetDrawTarget = delegate(int drawTarget) { int oldDrawTarget = mCurrentDrawTargetIndex; if (drawTarget == mCurrentDrawTargetIndex) return oldDrawTarget; if (drawTarget == MoSync.Constants.HANDLE_SCREEN) { mCurrentDrawTarget = mBackBuffer; mCurrentDrawTargetIndex = drawTarget; return oldDrawTarget; } Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, drawTarget); mCurrentDrawTarget = (WriteableBitmap)res.GetInternalObject(); mCurrentDrawTargetIndex = drawTarget; return oldDrawTarget; }; syscalls.maGetScrSize = delegate() { return MoSync.Util.CreateExtent(mBackBuffer.PixelWidth, mBackBuffer.PixelHeight); }; syscalls.maGetImageSize = delegate(int handle) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, handle); BitmapSource src = (BitmapSource)res.GetInternalObject(); if (src == null) MoSync.Util.CriticalError("maGetImageSize used with an invalid image resource."); int w = 0, h = 0; MoSync.Util.RunActionOnMainThreadSync(() => { w = src.PixelWidth; h = src.PixelHeight; }); return MoSync.Util.CreateExtent(w, h); }; syscalls.maDrawImage = delegate(int image, int left, int top) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, image); WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); Rect srcRect = new Rect(0, 0, src.PixelWidth, src.PixelHeight); Rect dstRect = new Rect(left, top, src.PixelWidth, src.PixelHeight); mCurrentDrawTarget.Blit(dstRect, src, srcRect, WriteableBitmapExtensions.BlendMode.Alpha); }; syscalls.maDrawImageRegion = delegate(int image, int srcRectPtr, int dstPointPtr, int transformMode) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, image); WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); Memory dataMemory = core.GetDataMemory(); int srcRectX = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.left); int srcRectY = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.top); int srcRectW = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.width); int srcRectH = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.height); int dstPointX = dataMemory.ReadInt32(dstPointPtr + MoSync.Struct.MAPoint2d.x); int dstPointY = dataMemory.ReadInt32(dstPointPtr + MoSync.Struct.MAPoint2d.y); Rect srcRect = new Rect(srcRectX, srcRectY, srcRectW, srcRectH); Rect dstRect = new Rect(dstPointX, dstPointY, srcRectW, srcRectH); GraphicsUtil.DrawImageRegion(mCurrentDrawTarget, dstPointX, dstPointY, srcRect, src, transformMode, mClipRect); }; syscalls.maCreateDrawableImage = delegate(int placeholder, int width, int height) { Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, placeholder); res.SetResourceType(MoSync.Constants.RT_IMAGE); WriteableBitmap bitmap = null; MoSync.Util.RunActionOnMainThreadSync(() => { bitmap = new WriteableBitmap(width, height); for (int i = 0; i < bitmap.PixelWidth * bitmap.PixelHeight; i++) { bitmap.Pixels[i] = (int)(0xff << 24); } }); if (bitmap == null) return MoSync.Constants.RES_OUT_OF_MEMORY; res.SetInternalObject(bitmap); return MoSync.Constants.RES_OK; }; syscalls.maCreateImageRaw = delegate(int _placeholder, int _src, int _size, int _alpha) { int width = MoSync.Util.ExtentX(_size); int height = MoSync.Util.ExtentY(_size); WriteableBitmap bitmap = null; MoSync.Util.RunActionOnMainThreadSync(() => { bitmap = new WriteableBitmap(width, height); }); //core.GetDataMemory().ReadIntegers(bitmap.Pixels, _src, width * height); bitmap.FromByteArray(core.GetDataMemory().GetData(), _src, width * height * 4); if (_alpha == 0) { int[] pixels = bitmap.Pixels; int numPixels = width * height; for (int i = 0; i < numPixels; i++) { pixels[i] = (int)((uint)pixels[i] | 0xff000000); } } Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, _placeholder); res.SetInternalObject(bitmap); res.SetResourceType(MoSync.Constants.RT_IMAGE); return MoSync.Constants.RES_OK; }; syscalls.maDrawRGB = delegate(int _dstPoint, int _src, int _srcRect, int _scanlength) { Memory dataMemory = core.GetDataMemory(); int dstX = dataMemory.ReadInt32(_dstPoint + MoSync.Struct.MAPoint2d.x); int dstY = dataMemory.ReadInt32(_dstPoint + MoSync.Struct.MAPoint2d.y); int srcRectX = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.left); int srcRectY = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.top); int srcRectW = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.width); int srcRectH = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.height); int[] pixels = mCurrentDrawTarget.Pixels; // todo: clipRect _scanlength *= 4; // sizeof(int) for (int h = 0; h < srcRectH; h++) { int pixelIndex = dstY * mCurrentDrawTarget.PixelWidth + dstX; int address = _src + (srcRectY + h) * _scanlength; for (int w = 0; w < srcRectW; w++) { uint srcPixel = dataMemory.ReadUInt32(address); uint dstPixel = (uint)pixels[pixelIndex]; uint srcPixelR = (srcPixel & 0x00ff0000) >> 16; uint srcPixelG = (srcPixel & 0x0000ff00) >> 8; uint srcPixelB = (srcPixel & 0x000000ff) >> 0; uint srcPixelA = (srcPixel & 0xff000000) >> 24; uint dstPixelR = (dstPixel & 0x00ff0000) >> 16; uint dstPixelG = (dstPixel & 0x0000ff00) >> 8; uint dstPixelB = (dstPixel & 0x000000ff) >> 0; uint dstPixelA = (dstPixel & 0xff000000) >> 24; dstPixelR += ((srcPixelR - dstPixelR) * srcPixelA) / 255; dstPixelG += ((srcPixelG - dstPixelG) * srcPixelA) / 255; dstPixelB += ((srcPixelB - dstPixelB) * srcPixelA) / 255; dstPixel = (dstPixelA << 24) | (dstPixelR << 16) | (dstPixelG << 8) | (dstPixelB); pixels[pixelIndex] = (int)dstPixel; address += 4; pixelIndex++; } dstY++; } }; syscalls.maGetImageData = delegate(int _image, int _dst, int _srcRect, int _scanlength) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, _image); WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); Memory dataMemory = core.GetDataMemory(); int srcRectX = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.left); int srcRectY = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.top); int srcRectW = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.width); int srcRectH = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.height); int lineDst = _dst; byte[] data = src.ToByteArray(srcRectY * src.PixelWidth, srcRectH * src.PixelWidth); // BlockCopy? byte[] coreArray = dataMemory.GetData(); for (int y = 0; y < srcRectH; y++) { System.Array.Copy(data, y * src.PixelWidth * 4, coreArray, lineDst, src.PixelWidth * 4); lineDst += _scanlength * 4; } }; syscalls.maCreateImageFromData = delegate(int _placeholder, int _data, int _offset, int _size) { Resource res = runtime.GetResource(MoSync.Constants.RT_BINARY, _data); if (res == null) return MoSync.Constants.RES_BAD_INPUT; Stream bin = (Stream)res.GetInternalObject(); if (bin == null) return MoSync.Constants.RES_BAD_INPUT; BoundedStream s = new BoundedStream(bin, _offset, _size); WriteableBitmap bitmap = MoSync.Util.CreateWriteableBitmapFromStream(s); s.Close(); if (bitmap == null) return MoSync.Constants.RES_BAD_INPUT; Resource imageRes = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, _placeholder); imageRes.SetInternalObject(bitmap); imageRes.SetResourceType(MoSync.Constants.RT_IMAGE); return MoSync.Constants.RES_OK; }; }
public static void SaveTileImages(string channelName, Uri image) { var bitmapImage = new BitmapImage() { CreateOptions = BitmapCreateOptions.None }; bitmapImage.ImageFailed += (s, e) => { }; bitmapImage.ImageOpened += (s, e) => { #region Create Image with Live Logo using (IsolatedStorageFile local = IsolatedStorageFile.GetUserStoreForApplication()) { // Create a new folder named DataFolder. if (!local.DirectoryExists(imageFolder)) local.CreateDirectory(imageFolder); var writeableBitmap = new WriteableBitmap(s as BitmapImage); writeableBitmap.Invalidate(); string offlineFilePath = System.IO.Path.Combine(imageFolder, channelName + ".jpg"); using (var imageOfflineFileStream = new IsolatedStorageFileStream(offlineFilePath, System.IO.FileMode.Create, local)) { writeableBitmap.SaveJpeg(imageOfflineFileStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 100); } BitmapImage icon = new BitmapImage(); icon.CreateOptions = BitmapCreateOptions.None; icon.UriSource = new Uri("/Assets/live_icon.png", UriKind.Relative); WriteableBitmap iconWB = new WriteableBitmap(icon); int AnsX1 = 175; int AnsY1 = 0; int AnsX2 = 300; int AnsY2 = 125; Rect sourceRect = new Rect(0, 0, iconWB.PixelWidth, iconWB.PixelHeight); Rect destRect = new Rect(AnsX1, AnsY1, AnsX2 - AnsX1, AnsY2 - AnsY1); writeableBitmap.Blit(destRect, iconWB, sourceRect); writeableBitmap.Invalidate(); string onlineFilePath = System.IO.Path.Combine(imageFolder, "live-" + channelName + ".jpg"); using (var imageOnlineFileStream = new IsolatedStorageFileStream(onlineFilePath, System.IO.FileMode.Create, local)) { writeableBitmap.SaveJpeg(imageOnlineFileStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 100); } } #endregion }; Deployment.Current.Dispatcher.BeginInvoke(() => { bitmapImage.UriSource = image; }); }
private static void DrawImage(WriteableBitmap targetBitmap, WriteableBitmap bitmap, Rect dest, TileInfo tile, MemoryCache<Feature> memoryCache, double opacity) { try { //todo: look at GDI rendering to deal with clipping var destRounded = RoundToPixel(dest); var sourceRect = new Rect(0, 0, 256, 256); opacity = 1; // hack, opacity not supported // Create the opacity color which sets the transparencey of the blitted image. var color = Color.FromArgb((byte)Convert.ToInt32(255 * opacity), 255, 255, 255); //todo: rethink opacity. There is opacity at layer level, and could be at tile level. targetBitmap.Blit(destRounded, bitmap, sourceRect, color, WriteableBitmapExtensions.BlendMode.Alpha); //!!!targetBitmap.DrawRectangle((int)destRounded.Left, (int)destRounded.Top, (int)destRounded.Right, (int)destRounded.Bottom, Colors.Red); } catch (Exception ex) { // todo report error Console.WriteLine(ex.Message); memoryCache.Remove(tile.Index); } }
private void DrawGrid(WriteableBitmap writeableBitmap) { Color color = Colors.White; color.A = 50; if (OverlayActivated) { if (SelectedOverlay != null && File.Exists(SelectedOverlay)) { if (_overlayImage == null) { BitmapImage bitmapSource = new BitmapImage(); bitmapSource.DecodePixelWidth = writeableBitmap.PixelWidth; bitmapSource.BeginInit(); bitmapSource.UriSource = new Uri(SelectedOverlay); bitmapSource.EndInit(); _overlayImage = BitmapFactory.ConvertToPbgra32Format(bitmapSource); _overlayImage.Freeze(); } int x = writeableBitmap.PixelWidth * OverlayScale / 100; int y = writeableBitmap.PixelHeight * OverlayScale / 100; int xx = writeableBitmap.PixelWidth * OverlayHorizontal / 100; int yy = writeableBitmap.PixelWidth * OverlayVertical / 100; writeableBitmap.Blit( new Rect(0 + (x / 2) + xx, 0 + (y / 2) + yy, writeableBitmap.PixelWidth - x, writeableBitmap.PixelHeight - y), _overlayImage, new Rect(0, 0, _overlayImage.PixelWidth, _overlayImage.PixelHeight)); } } switch (GridType) { case 1: { for (int i = 1; i < 3; i++) { writeableBitmap.DrawLine(0, (int) ((writeableBitmap.Height/3)*i), (int) writeableBitmap.Width, (int) ((writeableBitmap.Height/3)*i), color); writeableBitmap.DrawLine((int) ((writeableBitmap.Width/3)*i), 0, (int) ((writeableBitmap.Width/3)*i), (int) writeableBitmap.Height, color); } writeableBitmap.SetPixel((int) (writeableBitmap.Width/2), (int) (writeableBitmap.Height/2), 128, Colors.Red); } break; case 2: { for (int i = 1; i < 10; i++) { writeableBitmap.DrawLine(0, (int) ((writeableBitmap.Height/10)*i), (int) writeableBitmap.Width, (int) ((writeableBitmap.Height/10)*i), color); writeableBitmap.DrawLine((int) ((writeableBitmap.Width/10)*i), 0, (int) ((writeableBitmap.Width/10)*i), (int) writeableBitmap.Height, color); } writeableBitmap.SetPixel((int) (writeableBitmap.Width/2), (int) (writeableBitmap.Height/2), 128, Colors.Red); } break; case 3: { writeableBitmap.DrawLineDDA(0, 0, (int) writeableBitmap.Width, (int) writeableBitmap.Height, color); writeableBitmap.DrawLineDDA(0, (int) writeableBitmap.Height, (int) writeableBitmap.Width, 0, color); writeableBitmap.SetPixel((int) (writeableBitmap.Width/2), (int) (writeableBitmap.Height/2), 128, Colors.Red); } break; case 4: { writeableBitmap.DrawLineDDA(0, (int) (writeableBitmap.Height/2), (int) writeableBitmap.Width, (int) (writeableBitmap.Height/2), color); writeableBitmap.DrawLineDDA((int) (writeableBitmap.Width/2), 0, (int) (writeableBitmap.Width/2), (int) writeableBitmap.Height, color); writeableBitmap.SetPixel((int) (writeableBitmap.Width/2), (int) (writeableBitmap.Height/2), 128, Colors.Red); } break; default: break; } if (ShowRuler) { int x1 = writeableBitmap.PixelWidth*HorizontalMin/100; int x2 = writeableBitmap.PixelWidth*HorizontalMax/100; int y2 = writeableBitmap.PixelHeight*(100-VerticalMin)/100; int y1 = writeableBitmap.PixelHeight*(100-VerticalMax)/100; FillRectangle2(writeableBitmap, 0, 0, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, Color.FromArgb(128, 128, 128, 128)); FillRectangleDeBlend(writeableBitmap, x1, y1, x2, y2, Color.FromArgb(128, 128, 128, 128)); writeableBitmap.DrawRectangle( x1, y1, x2, y2, color); } }
private void DrawGrid(WriteableBitmap writeableBitmap) { Color color = Colors.White; color.A = 50; switch (GridType) { case 1: { for (int i = 1; i < 3; i++) { writeableBitmap.DrawLine(0, (int) ((writeableBitmap.Height/3)*i), (int) writeableBitmap.Width, (int) ((writeableBitmap.Height/3)*i), color); writeableBitmap.DrawLine((int) ((writeableBitmap.Width/3)*i), 0, (int) ((writeableBitmap.Width/3)*i), (int) writeableBitmap.Height, color); } writeableBitmap.SetPixel((int) (writeableBitmap.Width/2), (int) (writeableBitmap.Height/2), 128, Colors.Red); } break; case 2: { for (int i = 1; i < 10; i++) { writeableBitmap.DrawLine(0, (int) ((writeableBitmap.Height/10)*i), (int) writeableBitmap.Width, (int) ((writeableBitmap.Height/10)*i), color); writeableBitmap.DrawLine((int) ((writeableBitmap.Width/10)*i), 0, (int) ((writeableBitmap.Width/10)*i), (int) writeableBitmap.Height, color); } writeableBitmap.SetPixel((int) (writeableBitmap.Width/2), (int) (writeableBitmap.Height/2), 128, Colors.Red); } break; case 3: { writeableBitmap.DrawLineDDA(0, 0, (int) writeableBitmap.Width, (int) writeableBitmap.Height, color); writeableBitmap.DrawLineDDA(0, (int) writeableBitmap.Height, (int) writeableBitmap.Width, 0, color); writeableBitmap.SetPixel((int) (writeableBitmap.Width/2), (int) (writeableBitmap.Height/2), 128, Colors.Red); } break; case 4: { writeableBitmap.DrawLineDDA(0, (int) (writeableBitmap.Height/2), (int) writeableBitmap.Width, (int) (writeableBitmap.Height/2), color); writeableBitmap.DrawLineDDA((int) (writeableBitmap.Width/2), 0, (int) (writeableBitmap.Width/2), (int) writeableBitmap.Height, color); writeableBitmap.SetPixel((int) (writeableBitmap.Width/2), (int) (writeableBitmap.Height/2), 128, Colors.Red); } break; default: try { if (GridType > 4) { string filename = Path.Combine(ServiceProvider.Settings.OverlayFolder, SelectedOverlay + ".png"); if (File.Exists(filename)) { BitmapImage bitmapSource = new BitmapImage(); bitmapSource.BeginInit(); bitmapSource.UriSource = new Uri(filename); bitmapSource.EndInit(); WriteableBitmap overlay = BitmapFactory.ConvertToPbgra32Format(bitmapSource); writeableBitmap.Blit( new Rect(0, 0, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight), overlay, new Rect(0, 0, overlay.PixelWidth, overlay.PixelHeight)); } } } catch (Exception) { } break; } }
public void Draw(WriteableBitmap renderTarget) { // 从图中选出当前需要绘制的帧 var frame = _count / Slot; var rect = new Rect((frame % 5) * 116, (frame % 15) / 5 * 116, Width, Height); // 在画布上绘制 renderTarget.Blit(new Rect(_x, _y, Width, Height), Image, rect, WriteableBitmapExtensions.BlendMode.Alpha); // 动画播放结束后,终止自己的生命周期 if (frame == 14) _isAlive = false; _count++; }
private void ProcessImage(WriteableBitmap writeable) { var verySimpleBitmap = new VerySimpleBitmap(writeable.PixelWidth, writeable.PixelHeight); writeable.Pixels.CopyTo(verySimpleBitmap.Pixels, 0); var detectedSegments = faceLight.Process(verySimpleBitmap); var percentage = 0; switch (detectedSegments.Count()) { case 0: percentage = 0; break; case 1: percentage = 90; break; default: percentage = 99; break; } Debug.WriteLine("{0} segments detected", detectedSegments.Count()); foreach (var segment in detectedSegments) { if (segment.Width > 0 && segment.Height > 0) { // Uses the segment's center and half width, height var c = segment.Center; Debug.WriteLine("Center ({0} ,{1}), size ({2}, {3})", c.X, c.Y, segment.Width, segment.Height); var destRect = new Rect(segment.Center.X - segment.Width / 2, segment.Center.Y - segment.Height / 2, segment.Width, segment.Height); var sourceRect = new Rect(0, 0, _chimpWriteableBitmap.PixelWidth, _chimpWriteableBitmap.PixelHeight); writeable.Blit(destRect, _chimpWriteableBitmap, sourceRect); } } writeable.Invalidate(); SelectedImage.Source = writeable; writeable.SaveToFile(AppConstants.IsoFileName, (stream) => { /* do nothing */ }); ShowDisplay(DisplayState.Finished); NavigateToPhotoPage(percentage); }
private void DrawGrid(WriteableBitmap bitmap) { if (CentralPoint.X == 0 && CentralPoint.Y == 0) { CentralPoint = new Point(bitmap.PixelWidth/2, bitmap.PixelHeight/2); } WriteableBitmap tempbitmap = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight, bitmap.DpiX, bitmap.DpiY, PixelFormats.Pbgra32, bitmap.Palette); tempbitmap.DrawLine(0, (int) CentralPoint.Y - 5, bitmap.PixelWidth, (int) CentralPoint.Y - 5, Colors.White); tempbitmap.DrawLine(0, (int) CentralPoint.Y + 5, bitmap.PixelWidth, (int) CentralPoint.Y + 5, Colors.White); tempbitmap.DrawLine((int) CentralPoint.X - 5, 0, (int) CentralPoint.X - 5, bitmap.PixelHeight, Colors.White); tempbitmap.DrawLine((int) CentralPoint.X + 5, 0, (int) CentralPoint.X + 5, bitmap.PixelHeight, Colors.White); bitmap.Blit(new Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), tempbitmap, new Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight)); DrawPreviewImage(bitmap); }
private void DrawText(WriteableBitmap wBmp, String text, int size, int x, int y) { // Build text FormattedText fText = new FormattedText(text, culture, FlowDirection.LeftToRight, new Typeface(this.FontFamily, FontStyles.Normal, FontWeights.Bold, stretch), size, Brushes.White); // Draw text DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); drawingContext.DrawText(fText, new System.Windows.Point(2, 2)); drawingContext.Close(); // Build BMP RenderTargetBitmap tBmp = new RenderTargetBitmap(180, 180, 120, 96, PixelFormats.Pbgra32); tBmp.Render(drawingVisual); wBmp.Blit(new Point(x, y), new WriteableBitmap(tBmp), new Rect(0, 0, tBmp.PixelWidth, tBmp.PixelHeight), Colors.White, WriteableBitmapExtensions.BlendMode.Alpha); }
private void DrawGrid(WriteableBitmap writeableBitmap) { System.Windows.Media.Color color = Colors.White; color.A = 50; if (OverlayActivated) { if ((SelectedOverlay != null && File.Exists(SelectedOverlay)) || OverlayUseLastCaptured) { if (OverlayUseLastCaptured) { if (File.Exists(ServiceProvider.Settings.SelectedBitmap.FileItem.LargeThumb) && _lastOverlay != ServiceProvider.Settings.SelectedBitmap.FileItem.LargeThumb) { _lastOverlay = ServiceProvider.Settings.SelectedBitmap.FileItem.LargeThumb; _overlayImage = null; } } if (_overlayImage == null) { BitmapImage bitmapSource = new BitmapImage(); bitmapSource.DecodePixelWidth = writeableBitmap.PixelWidth; bitmapSource.BeginInit(); bitmapSource.UriSource = new Uri(OverlayUseLastCaptured ? _lastOverlay : SelectedOverlay); bitmapSource.EndInit(); _overlayImage = BitmapFactory.ConvertToPbgra32Format(bitmapSource); _overlayImage.Freeze(); } int x = writeableBitmap.PixelWidth*OverlayScale/100; int y = writeableBitmap.PixelHeight*OverlayScale/100; int xx = writeableBitmap.PixelWidth*OverlayHorizontal/100; int yy = writeableBitmap.PixelWidth*OverlayVertical/100; System.Windows.Media.Color transpColor = Colors.White; //set color transparency for blit only the alpha chanel is used from transpColor if (OverlayTransparency < 100) transpColor = System.Windows.Media.Color.FromArgb((byte) (0xff*OverlayTransparency/100d), 0xff, 0xff, 0xff); writeableBitmap.Blit( new Rect(0 + (x / 2) + xx, 0 + (y / 2) + yy, writeableBitmap.PixelWidth - x, writeableBitmap.PixelHeight - y), _overlayImage, new Rect(0, 0, _overlayImage.PixelWidth, _overlayImage.PixelHeight), transpColor, WriteableBitmapExtensions.BlendMode.Alpha); } } switch (GridType) { case 1: { for (int i = 1; i < 3; i++) { writeableBitmap.DrawLine(0, (int)((writeableBitmap.Height / 3) * i), (int)writeableBitmap.Width, (int)((writeableBitmap.Height / 3) * i), color); writeableBitmap.DrawLine((int)((writeableBitmap.Width / 3) * i), 0, (int)((writeableBitmap.Width / 3) * i), (int)writeableBitmap.Height, color); } writeableBitmap.SetPixel((int)(writeableBitmap.Width / 2), (int)(writeableBitmap.Height / 2), 128, Colors.Red); } break; case 2: { for (int i = 1; i < 10; i++) { writeableBitmap.DrawLine(0, (int)((writeableBitmap.Height / 10) * i), (int)writeableBitmap.Width, (int)((writeableBitmap.Height / 10) * i), color); writeableBitmap.DrawLine((int)((writeableBitmap.Width / 10) * i), 0, (int)((writeableBitmap.Width / 10) * i), (int)writeableBitmap.Height, color); } writeableBitmap.SetPixel((int)(writeableBitmap.Width / 2), (int)(writeableBitmap.Height / 2), 128, Colors.Red); } break; case 3: { writeableBitmap.DrawLineDDA(0, 0, (int)writeableBitmap.Width, (int)writeableBitmap.Height, color); writeableBitmap.DrawLineDDA(0, (int)writeableBitmap.Height, (int)writeableBitmap.Width, 0, color); writeableBitmap.SetPixel((int)(writeableBitmap.Width / 2), (int)(writeableBitmap.Height / 2), 128, Colors.Red); } break; case 4: { writeableBitmap.DrawLineDDA(0, (int)(writeableBitmap.Height / 2), (int)writeableBitmap.Width, (int)(writeableBitmap.Height / 2), color); writeableBitmap.DrawLineDDA((int)(writeableBitmap.Width / 2), 0, (int)(writeableBitmap.Width / 2), (int)writeableBitmap.Height, color); writeableBitmap.SetPixel((int)(writeableBitmap.Width / 2), (int)(writeableBitmap.Height / 2), 128, Colors.Red); } break; default: break; } if (ShowRuler && NoSettingArea) { int x1 = writeableBitmap.PixelWidth * (HorizontalMin) / 1000; int x2 = writeableBitmap.PixelWidth * (HorizontalMin+HorizontalMax) / 1000; int y2 = writeableBitmap.PixelHeight * (VerticalMin+VerticalMax) / 1000; int y1 = writeableBitmap.PixelHeight * VerticalMin / 1000; writeableBitmap.FillRectangle2(0, 0, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, System.Windows.Media.Color.FromArgb(128, 128, 128, 128)); writeableBitmap.FillRectangleDeBlend(x1, y1, x2, y2, System.Windows.Media.Color.FromArgb(128, 128, 128, 128)); writeableBitmap.DrawRectangle(x1, y1, x2, y2, color); } }
private void DisplaySmilieResult(WriteableBitmap writeableBitmap, FaceRestAPI.FaceAPI result) { int countHappy = 0; int countSad = 0; foreach (var tag in result.photos[0].tags) { // Uses the segment's center and half width, height var c = tag.center; Debug.WriteLine("Center ({0} ,{1}), size ({2}, {3})", c.x, c.y, tag.width, tag.height); var destRect = new Rect(writeableBitmap.PixelWidth * 0.01 * (tag.center.x - tag.width / 2), writeableBitmap.PixelHeight * 0.01 * (tag.center.y - tag.height / 2), writeableBitmap.PixelWidth * 0.01 * tag.width, writeableBitmap.PixelHeight * 0.01 * tag.height); var whichFace = ChooseFace(tag.attributes); if (whichFace == _smilieWriteableBitmap) countHappy++; else countSad++; var sourceRect = new Rect(0, 0, whichFace.PixelWidth, whichFace.PixelHeight); writeableBitmap.Blit(destRect, whichFace, sourceRect); } writeableBitmap.Invalidate(); SelectedImage.Source = writeableBitmap; writeableBitmap.SaveToFile(AppConstants.IsoFileName, (stream) => { /* do nothing */ }); ShowDisplay(DisplayState.Finished); NavigateToPhotoPage(countHappy, countSad); }
public void Init(Syscalls syscalls, Core core, Runtime runtime) { PhoneApplicationFrame frame = (PhoneApplicationFrame)Application.Current.RootVisual; double screenWidth = System.Windows.Application.Current.Host.Content.ActualWidth; double screenHeight = System.Windows.Application.Current.Host.Content.ActualHeight; if ((int)screenHeight == 0) throw new Exception("screenHeight"); PhoneApplicationPage mainPage = (PhoneApplicationPage)frame.Content; Image mainImage = new Image(); mainPage.Width = screenWidth; mainPage.Height = screenHeight; mainImage.Width = screenWidth; mainImage.Height = screenHeight; mainPage.Content = mainImage; // no apparent effect on memory leaks. runtime.RegisterCleaner(delegate() { MoSync.Util.RunActionOnMainThreadSync(() => { mainPage.Content = null; }); }); mBackBuffer = new WriteableBitmap( (int)screenWidth, (int)screenHeight); mFrontBuffer = new WriteableBitmap( (int)screenWidth, (int)screenHeight); mainImage.Source = mFrontBuffer; mCurrentDrawTarget = mBackBuffer; mCurrentWindowsColor = System.Windows.Media.Color.FromArgb(0xff, (byte)(mCurrentColor >> 16), (byte)(mCurrentColor >> 8), (byte)(mCurrentColor)); syscalls.maSetColor = delegate(int rgb) { int oldColor = (int)mCurrentColor; mCurrentColor = 0xff000000 | (uint)(rgb & 0xffffff); mCurrentWindowsColor = System.Windows.Media.Color.FromArgb(0xff, (byte)(mCurrentColor >> 16), (byte)(mCurrentColor >> 8), (byte)(mCurrentColor)); return oldColor & 0xffffff; }; syscalls.maSetClipRect = delegate(int x, int y, int w, int h) { }; syscalls.maGetClipRect = delegate(int cliprect) { }; syscalls.maPlot = delegate(int x, int y) { mCurrentDrawTarget.SetPixel(x, y, (int)mCurrentColor); }; syscalls.maUpdateScreen = delegate() { System.Array.Copy(mBackBuffer.Pixels, mFrontBuffer.Pixels, mFrontBuffer.PixelWidth * mFrontBuffer.PixelHeight); InvalidateWriteableBitmapOnMainThread(mFrontBuffer); }; syscalls.maFillRect = delegate(int x, int y, int w, int h) { mCurrentDrawTarget.FillRectangle(x, y, x + w, y + h, (int)mCurrentColor); }; syscalls.maLine = delegate(int x1, int y1, int x2, int y2) { mCurrentDrawTarget.DrawLine(x1, y1, x2, y2, (int)mCurrentColor); }; TextBlock textBlock = new TextBlock(); textBlock.FontSize = mCurrentFontSize; syscalls.maDrawText = delegate(int left, int top, int str) { String text = core.GetDataMemory().ReadStringAtAddress(str); if (text.Length == 0) return; MoSync.Util.RunActionOnMainThreadSync(() => { textBlock.Text = text; textBlock.Foreground = new SolidColorBrush(mCurrentWindowsColor); WriteableBitmap b = new WriteableBitmap(textBlock, null); mCurrentDrawTarget.Blit(new Rect(left, top, b.PixelWidth, b.PixelHeight), b, new Rect(0, 0, b.PixelWidth, b.PixelHeight)); }); }; syscalls.maGetTextSize = delegate(int str) { String text = core.GetDataMemory().ReadStringAtAddress(str); int textWidth = 0; int textHeight = 0; MoSync.Util.RunActionOnMainThreadSync(() => { textBlock.Text = text; textWidth = (int)textBlock.ActualWidth; textHeight = (int)textBlock.ActualHeight; }); return MoSync.Util.CreateExtent(textWidth, textHeight); }; syscalls.maDrawTextW = delegate(int left, int top, int str) { String text = core.GetDataMemory().ReadWStringAtAddress(str); if (text.Length == 0) return; MoSync.Util.RunActionOnMainThreadSync(() => { textBlock.Text = text; textBlock.Foreground = new SolidColorBrush(mCurrentWindowsColor); WriteableBitmap b = new WriteableBitmap(textBlock, null); Rect dstRect = new Rect(left, top, b.PixelWidth, b.PixelHeight); Rect srcRect = new Rect(0, 0, b.PixelWidth, b.PixelHeight); // cliprect.. Rect clipRect = new Rect(0, 0, mBackBuffer.PixelWidth, mBackBuffer.PixelHeight); clipRect.Intersect(dstRect); if (clipRect.IsEmpty == true) { return; } mCurrentDrawTarget.Blit(dstRect, b, srcRect); }); }; syscalls.maGetTextSizeW = delegate(int str) { String text = core.GetDataMemory().ReadWStringAtAddress(str); int textWidth = 0; int textHeight = 0; MoSync.Util.RunActionOnMainThreadSync(() => { textBlock.Text = text; textWidth = (int)textBlock.ActualWidth; textHeight = (int)textBlock.ActualHeight; }); return MoSync.Util.CreateExtent(textWidth, textHeight); }; syscalls.maFillTriangleFan = delegate(int points, int count) { int[] newPoints = new int[count * 2 + 2]; for (int i = 0; i < count; i++) { newPoints[i * 2 + 0] = core.GetDataMemory().ReadInt32(points + i * 8); newPoints[i * 2 + 1] = core.GetDataMemory().ReadInt32(points + i * 8 + 4); } newPoints[count * 2 + 0] = core.GetDataMemory().ReadInt32(points + 0); newPoints[count * 2 + 1] = core.GetDataMemory().ReadInt32(points + 4); mCurrentDrawTarget.FillPolygon(newPoints, (int)mCurrentColor); }; syscalls.maFillTriangleStrip = delegate(int points, int count) { int[] xcoords = new int[count]; int[] ycoords = new int[count]; for (int i = 0; i < count; i++) { xcoords[i] = core.GetDataMemory().ReadInt32(points + i * 8); ycoords[i] = core.GetDataMemory().ReadInt32(points + i * 8 + 4); } for (int i = 2; i < count; i++) { mCurrentDrawTarget.FillTriangle( xcoords[i - 2], ycoords[i - 2], xcoords[i - 1], ycoords[i - 1], xcoords[i - 0], ycoords[i - 0], (int)mCurrentColor); } }; syscalls.maSetDrawTarget = delegate(int drawTarget) { int oldDrawTarget = mCurrentDrawTargetIndex; if (drawTarget == mCurrentDrawTargetIndex) return oldDrawTarget; if (drawTarget == MoSync.Constants.HANDLE_SCREEN) { mCurrentDrawTarget = mBackBuffer; mCurrentDrawTargetIndex = drawTarget; return oldDrawTarget; } Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, drawTarget); mCurrentDrawTarget = (WriteableBitmap)res.GetInternalObject(); mCurrentDrawTargetIndex = drawTarget; return oldDrawTarget; }; syscalls.maGetScrSize = delegate() { return MoSync.Util.CreateExtent(mBackBuffer.PixelWidth, mBackBuffer.PixelHeight); }; syscalls.maGetImageSize = delegate(int handle) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, handle); BitmapSource src = (BitmapSource)res.GetInternalObject(); int w = 0, h = 0; MoSync.Util.RunActionOnMainThreadSync(() => { w = src.PixelWidth; h = src.PixelHeight; }); return MoSync.Util.CreateExtent(w, h); }; syscalls.maDrawImage = delegate(int image, int left, int top) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, image); WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); Rect srcRect = new Rect(0, 0, src.PixelWidth, src.PixelHeight); Rect dstRect = new Rect(left, top, src.PixelWidth, src.PixelHeight); mCurrentDrawTarget.Blit(dstRect, src, srcRect, WriteableBitmapExtensions.BlendMode.Alpha); }; syscalls.maDrawImageRegion = delegate(int image, int srcRectPtr, int dstPointPtr, int transformMode) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, image); WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); Memory dataMemory = core.GetDataMemory(); int srcRectX = dataMemory.ReadInt32(srcRectPtr + 0); int srcRectY = dataMemory.ReadInt32(srcRectPtr + 4); int srcRectW = dataMemory.ReadInt32(srcRectPtr + 8); int srcRectH = dataMemory.ReadInt32(srcRectPtr + 12); int dstPointX = dataMemory.ReadInt32(dstPointPtr + 0); int dstPointY = dataMemory.ReadInt32(dstPointPtr + 4); Rect srcRect = new Rect(srcRectX, srcRectY, srcRectW, srcRectH); Rect dstRect = new Rect(dstPointX, dstPointY, srcRectW, srcRectH); // mCurrentDrawTarget.Blit(dstRect, src, srcRect, WriteableBitmapExtensions.BlendMode.Alpha); DrawImageRegion(mCurrentDrawTarget, dstPointX, dstPointY, srcRect, src, transformMode); }; syscalls.maCreateDrawableImage = delegate(int placeholder, int width, int height) { Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, placeholder); res.SetResourceType(MoSync.Constants.RT_IMAGE); WriteableBitmap bitmap = null; MoSync.Util.RunActionOnMainThreadSync(() => { bitmap = new WriteableBitmap(width, height); }); if (bitmap == null) return MoSync.Constants.RES_OUT_OF_MEMORY; res.SetInternalObject(bitmap); return MoSync.Constants.RES_OK; }; syscalls.maCreateImageRaw = delegate(int _placeholder, int _src, int _size, int _alpha) { int width = MoSync.Util.ExtentX(_size); int height = MoSync.Util.ExtentY(_size); WriteableBitmap bitmap = null; MoSync.Util.RunActionOnMainThreadSync(() => { bitmap = new WriteableBitmap(width, height); }); //core.GetDataMemory().ReadIntegers(bitmap.Pixels, _src, width * height); bitmap.FromByteArray(core.GetDataMemory().GetData(), _src, width * height * 4); if (_alpha == 0) { int[] pixels = bitmap.Pixels; int numPixels = width * height; for (int i = 0; i < numPixels; i++) { pixels[i] = (int)((uint)pixels[i] | 0xff000000); } } runtime.SetResource(_placeholder, new Resource( bitmap, MoSync.Constants.RT_IMAGE ) ); return MoSync.Constants.RES_OK; }; syscalls.maDrawRGB = delegate(int _dstPoint, int _src, int _srcRect, int _scanlength) { Memory dataMemory = core.GetDataMemory(); int dstX = dataMemory.ReadInt32(_dstPoint + 0); int dstY = dataMemory.ReadInt32(_dstPoint + 4); int srcRectX = dataMemory.ReadInt32(_srcRect + 0); int srcRectY = dataMemory.ReadInt32(_srcRect + 4); int srcRectW = dataMemory.ReadInt32(_srcRect + 8); int srcRectH = dataMemory.ReadInt32(_srcRect + 12); int[] pixels = mCurrentDrawTarget.Pixels; // todo: clipRect _scanlength *= 4; // sizeof(int) for (int h = 0; h < srcRectH; h++) { int pixelIndex = dstY * mCurrentDrawTarget.PixelWidth + dstX; int address = _src + (srcRectY + h) * _scanlength; for (int w = 0; w < srcRectW; w++) { uint srcPixel = dataMemory.ReadUInt32(address); uint dstPixel = (uint)pixels[pixelIndex]; uint srcPixelR = (srcPixel & 0x00ff0000) >> 16; uint srcPixelG = (srcPixel & 0x0000ff00) >> 8; uint srcPixelB = (srcPixel & 0x000000ff) >> 0; uint srcPixelA = (srcPixel & 0xff000000) >> 24; uint dstPixelR = (dstPixel & 0x00ff0000) >> 16; uint dstPixelG = (dstPixel & 0x0000ff00) >> 8; uint dstPixelB = (dstPixel & 0x000000ff) >> 0; uint dstPixelA = (dstPixel & 0xff000000) >> 24; dstPixelR += ((srcPixelR - dstPixelR) * srcPixelA) / 255; dstPixelG += ((srcPixelG - dstPixelG) * srcPixelA) / 255; dstPixelB += ((srcPixelB - dstPixelB) * srcPixelA) / 255; dstPixel = (dstPixelA << 24) | (dstPixelR << 16) | (dstPixelG << 8) | (dstPixelB); pixels[pixelIndex] = (int)dstPixel; address += 4; pixelIndex++; } dstY++; } }; syscalls.maGetImageData = delegate(int _image, int _dst, int _srcRect, int _scanlength) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, _image); WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); Memory dataMemory = core.GetDataMemory(); int srcRectX = dataMemory.ReadInt32(_srcRect + 0); int srcRectY = dataMemory.ReadInt32(_srcRect + 4); int srcRectW = dataMemory.ReadInt32(_srcRect + 8); int srcRectH = dataMemory.ReadInt32(_srcRect + 12); int lineDst = _dst; byte[] data = src.ToByteArray(srcRectY * src.PixelWidth, srcRectH * src.PixelWidth); byte[] coreArray = dataMemory.GetData(); for (int y = 0; y < srcRectH; y++) { System.Array.Copy(data, y * src.PixelWidth * 4, coreArray, lineDst, src.PixelWidth * 4); lineDst += _scanlength * 4; } }; syscalls.maCreateImageFromData = delegate(int _placeholder, int _data, int _offset, int _size) { Resource res = runtime.GetResource(MoSync.Constants.RT_BINARY, _data); Memory mem = (Memory)res.GetInternalObject(); Stream s = mem.GetStream(_offset, _size); WriteableBitmap bitmap = MoSync.Util.CreateWriteableBitmapFromStream(s); s.Close(); runtime.SetResource( _placeholder, new Resource( bitmap, MoSync.Constants.RT_IMAGE ) ); return MoSync.Constants.RES_OK; }; }
private void DrawBefore(WriteableBitmap bitmap) { if (!WSRConfig.GetInstance().terminator) { return; } bitmap.Blit(new System.Windows.Point(0, 0), wBmp1, new Rect(0, 0, wBmp1.PixelWidth, wBmp1.PixelHeight), Colors.White, WriteableBitmapExtensions.BlendMode.Multiply); bitmap.Blit(new System.Windows.Point(0, 0), wBmp2, new Rect(0, 0, wBmp2.PixelWidth, wBmp2.PixelHeight), Colors.White, WriteableBitmapExtensions.BlendMode.Alpha); }