/// <summary> /// Check if the IDataObject has an image /// </summary> /// <param name="dataObject"></param> /// <returns>true if an image is there</returns> public static bool ContainsImage(IDataObject dataObject) { if (dataObject == null) { return(false); } if (dataObject.GetDataPresent(DataFormats.Bitmap) || dataObject.GetDataPresent(DataFormats.Dib) || dataObject.GetDataPresent(DataFormats.Tiff) || dataObject.GetDataPresent(DataFormats.EnhancedMetafile) || dataObject.GetDataPresent(FORMAT_PNG) || dataObject.GetDataPresent(FORMAT_17) || dataObject.GetDataPresent(FORMAT_JPG) || dataObject.GetDataPresent(FORMAT_GIF)) { return(true); } var imageFiles = GetImageFilenames(dataObject); if (imageFiles.Any()) { return(true); } if (!dataObject.GetDataPresent(FORMAT_FILECONTENTS)) { return(false); } try { var imageStream = dataObject.GetData(FORMAT_FILECONTENTS) as MemoryStream; if (IsValidStream(imageStream)) { using (BitmapHelper.FromStream(imageStream)) { // If we get here, there is an image return(true); } } } catch (Exception) { // Ignore } return(false); }
public unsafe void Convert_BitmapToPix_Format8bppIndexed() { var sourceFile = TestFilePath("Conversion/photo_palette_8bpp.png"); var bitmapConverter = new BitmapToPixConverter(); using (var source = new Bitmap(sourceFile)) { Assert.That(BitmapHelper.GetBPP(source), Is.EqualTo(8)); Assert.That(source.PixelFormat, Is.EqualTo(PixelFormat.Format8bppIndexed)); using (var dest = bitmapConverter.Convert(source)) { var destFilename = TestResultRunFile("Conversion/BitmapToPix_palette_8bpp.png"); dest.Save(destFilename, ImageFormat.Png); AssertAreEquivalent(source, dest, true); } } }
/// <summary> /// Initialize a new instanc eof <see cref="RwRasterDataStructNode"/> using a given bitmap and a PS2 <see cref="PS2.Graphics.PS2PixelFormat"/> to encode the bitmap to. /// </summary> /// <param name="bitmap">Bitmap to be encoded using the given pixel format.</param> /// <param name="pixelFormat">The pixel format the bitmap will be encoded to and stored in the texture data.</param> public RwRasterDataStructNode(Bitmap bitmap, PS2PixelFormat pixelFormat) : base(RwNodeId.RwStructNode) { if (PS2PixelFormatHelper.IsIndexedPixelFormat(pixelFormat)) { BitmapHelper.QuantizeBitmap(bitmap, PS2PixelFormatHelper.GetIndexedColorCount(pixelFormat), out mIndices, out mPalette); } else { mPixels = BitmapHelper.GetColors(bitmap) .Select(x => Color.FromArgb(PS2PixelFormatHelper.ScaleFullRangeAlphaToHalfRange(x.A), x.R, x.G, x.B)) .ToArray(); } mMipMapData = new byte[0]; }
private void ToSave() { //string name = "d"; //ToSave(name); //name = "h"; //ToSave(name); //name = "k"; //ToSave(name); //name = "m"; //ToSave(name); string temp = string.Format(@"C:\Users\Tinn\Desktop\pk\king.png"); Image image = BitmapHelper.GetBitmapFormFile(temp); image = BottomRight(image); image.Save(string.Format(@"d:\king.png")); }
public unsafe void Convert_ScaledBitmapToPix() { var sourceFilePath = TestFilePath("Conversion/photo_rgb_32bpp.tif"); var bitmapConverter = new BitmapToPixConverter(); using (var source = new Bitmap(sourceFilePath)) { using (var scaledSource = new Bitmap(source, new Size(source.Width * 2, source.Height * 2))) { Assert.That(BitmapHelper.GetBPP(scaledSource), Is.EqualTo(32)); using (var dest = bitmapConverter.Convert(scaledSource)) { dest.Save(TestResultRunFile("Conversion/ScaledBitmapToPix_rgb_32bpp.tif"), ImageFormat.Tiff); AssertAreEquivalent(scaledSource, dest, true); } } } }
private static int CutButton(BitmapHelper bitmap, int right) { for (int w = bitmap.colors.GetLength(0) - 1; w >= 0; w--) { for (int h = 0; h < bitmap.colors.GetLength(1); h++) { System.Drawing.Color color = bitmap.GetPixel(w, h); if (color.A > 10) { right = w; return(right); } } } return(right); }
private static int CutRight(BitmapHelper bitmap, int down) { for (int h = bitmap.colors.GetLength(1) - 1; h >= 0; h--) { for (int w = 0; w < bitmap.colors.GetLength(0); w++) { System.Drawing.Color color = bitmap.GetPixel(w, h); if (color.A > 10) { down = h; return(down); } } } return(down); }
private static unsafe void TransferDataFormat1bppIndexed(BitmapData imgData, PixData pixData) { int height = imgData.Height; int num = imgData.Width / 8; for (int index1 = 0; index1 < height; ++index1) { byte *data1 = (byte *)((IntPtr)(void *)imgData.Scan0 + index1 * imgData.Stride); uint *data2 = (uint *)((IntPtr)(void *)pixData.Data + (index1 * pixData.WordsPerLine) * 4); for (int index2 = 0; index2 < num; ++index2) { byte dataByte = BitmapHelper.GetDataByte(data1, index2); PixData.SetDataByte(data2, index2, (uint)dataByte); } } }
private void UpdateVM(IDbSpriteAtlasFromSpr entry) { var model = spriteAtlasDataProvider.GetSpriteSet(entry.Id); if (model != null) { FromModel(model); } CurrentSpriteIndex = 0; foreach (var item in Items) { BitmapHelper.SetPaletteColors(item.Image, Palette.Data); } }
// multi-threaded public void Add(Image image) { if (_imgs == null) { _imgs = new List <Img>(); } int availableHeight = (int)((_colWidth * 1f / image.Width) * image.Height); Image resized = BitmapHelper.ResizeImage(image, _colWidth, availableHeight); _imgs.Add(new Img(image, resized)); // TODO: When new image added into the list // ReDraw method will be invoked // => multiple redraw run at the same time. ReDraw(); }
/// <summary> /// Return an Image representing the Window! /// As GDI+ draws it, it will be without Aero borders! /// TODO: If there is a parent, this could be removed with SetParent, and set back afterwards. /// </summary> public static Bitmap PrintWindow(this IInteropWindow nativeWindow) { var returnBitmap = nativeWindow.PrintWindow <Bitmap>(); if (nativeWindow.HasParent || !nativeWindow.IsMaximized()) { return(returnBitmap); } Log.Debug().WriteLine("Correcting for maximalization"); Size borderSize = nativeWindow.GetInfo().BorderSize; var bounds = nativeWindow.GetInfo().Bounds; var borderRectangle = new NativeRect(borderSize.Width, borderSize.Height, bounds.Width - 2 * borderSize.Width, bounds.Height - 2 * borderSize.Height); BitmapHelper.Crop(ref returnBitmap, ref borderRectangle); return(returnBitmap); }
private static int CutLeft(BitmapHelper bitmap, int up) { for (int h = 0; h < bitmap.colors.GetLength(1); h++) { for (int w = 0; w < bitmap.colors.GetLength(0); w++) { System.Drawing.Color color = bitmap.GetPixel(w, h); if (color.A > 10) { up = h; return(up); } } } return(up); }
void btImage_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog() { Title = "Select Product Image", Filter = "Image Files|*.gif;*.bmp;*.jpg;*.jpeg;*.png;*.tga;*.tif;*.tiff|GIF file format|*.gif|BMP file format|*.bmp|JPEG file format|*.jpg;*.jpeg|PNG file format|*.png|TGA file format|*.tga|TIFF file format|*.tif;*.tiff", }; if (ofd.ShowDialog() == DialogResult.OK) { Bitmap image = BitmapHelper.GetBitmapFormFile(ofd.FileName); ImageForm iform = new ImageForm(image); iform.BackColor = Color.Black; iform.ShowDialog(this); } }
public ActionResult GetPicture(int id = -1) { if (id < 0) { return(HttpNotFound()); } var user = this.serviceUser.GetById(id); if (user != null) {//TODO : move this into the service layer System.IO.Stream stream = new System.IO.MemoryStream(); if (user.PictureUrl.Contains("/Content/")) { // Construct absolute image path var imagePath = Server.MapPath("~/Content/Images/NoAvatar.png"); var image = System.Drawing.Image.FromFile(imagePath); var resized = BitmapHelper.ResizeImage(image, 370, 211); stream = new System.IO.MemoryStream(); resized.Save(stream, System.Drawing.Imaging.ImageFormat.Png); stream.Position = 0; } else { //TODO : arggghghh this is ugly! try { string path = String.Format("{0}/avatars/thumbnail_200x200.png", user.Id); if (this.serviceLocalStorage.DownloadFile(ref stream, path, "users")) { stream = BitmapHelper.ResizeImage(stream, 370, 211); stream.Position = 0; } } catch (Exception exc) { this.serviceLog.LogError("ProfileController::GetPicture", exc); return(HttpNotFound()); } } return(base.File(stream, "image/png")); } return(HttpNotFound()); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.activity_languages_learn); var toolbarMain = FindViewById <Toolbar>(Resource.Id.toolbar_languages_learn); SetSupportActionBar(toolbarMain); SupportActionBar.SetDisplayHomeAsUpEnabled(true); var displayMetrics = new DisplayMetrics(); WindowManager.DefaultDisplay.GetRealMetrics(displayMetrics); using (var background = BitmapHelper.GetBackgroung(Resources, displayMetrics.WidthPixels - PixelConverter.DpToPX(70), PixelConverter.DpToPX(300))) { FindViewById <TextView>(Resource.Id.textView_learn_en).Background = background; } }
private unsafe void TransferDataFormat1bppIndexed(BitmapData imgData, PixData pixData) { var height = imgData.Height; var width = imgData.Width / 8; for (int y = 0; y < height; y++) { byte *imgLine = (byte *)imgData.Scan0 + (y * imgData.Stride); uint *pixLine = (uint *)pixData.Data + (y * pixData.WordsPerLine); for (int x = 0; x < width; x++) { byte pixelVal = BitmapHelper.GetDataByte(imgLine, x); PixData.SetDataByte(pixLine, x, pixelVal); } } }
public HeightmapDto Generate(int width, int height, int overlappedSize, int seed, float scale, int octaves, float persistance, float lacunarity, int offsetX = 0, int offsetZ = 0) { var noise = new OpenSimplexNoise(); float[,] heightmapData = GenerateHeightmap(width + overlappedSize * 2, height + overlappedSize * 2, seed, scale, octaves, persistance, lacunarity, offsetX - overlappedSize, offsetZ - overlappedSize); var heightmapByteArray = BitmapHelper.WriteToByteArray(heightmapData); return(new HeightmapDto { Width = width, Height = height, OverlappedSize = overlappedSize, HeightmapOriginalArray = heightmapData, HeightmapByteArray = heightmapByteArray }); }
public ObjMaterial GetPlainColor(ShaderProgram program, string aName, Color aColor) { if (materials.ContainsKey(aName)) { return(materials[aName]); } SolidBrush tempBrush = new SolidBrush(aColor); Bitmap tempBmp = BitmapHelper.CreatBitamp(new Size(20, 20), tempBrush); ObjMaterial tempMaterial = new ObjMaterial(program) { DiffuseMap = new Texture(tempBmp) }; AddMaterial(aName, tempMaterial); return(tempMaterial); }
public void BitmapStitcher_Default() { using (var bitmapStitcher = new BitmapStitcher()) { bitmapStitcher .AddBitmap(BitmapHelper.LoadBitmap(@"TestFiles\scroll0.png")) .AddBitmap(BitmapHelper.LoadBitmap(@"TestFiles\scroll35.png")) .AddBitmap(BitmapHelper.LoadBitmap(@"TestFiles\scroll70.png")) .AddBitmap(BitmapHelper.LoadBitmap(@"TestFiles\scroll105.png")) .AddBitmap(BitmapHelper.LoadBitmap(@"TestFiles\scroll124.png")); using (var completedBitmap = bitmapStitcher.Result()) { completedBitmap.Save("scroll.png", ImageFormat.Png); } } }
private void loadButton_Click(object sender, EventArgs e) { DialogResult loadResult = imageOpenFileDialog.ShowDialog(); if (loadResult == DialogResult.OK) { Reset(); inputBitmap = BitmapHelper.Load(imageOpenFileDialog.FileName); inputSurface = new Bitmap(inputBitmap); gInput = Graphics.FromImage(inputSurface); pictureBox.Image = inputSurface; } }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.activity_images_learn); SetSupportActionBar(FindViewById <Toolbar>(Resource.Id.toolbar_images_learn)); SupportActionBar.SetDisplayHomeAsUpEnabled(true); var displayMetrics = new DisplayMetrics(); WindowManager.DefaultDisplay.GetRealMetrics(displayMetrics); using (var background = BitmapHelper.GetBackgroung(Resources, displayMetrics.WidthPixels - PixelConverter.DpToPX(20), PixelConverter.DpToPX(300))) { FindViewById <LinearLayout>(Resource.Id.learn_background).Background = background; } Button_Images_Learn_Next_Click(null); }
public ToolBarViewModel() { Buttons = new ObservableCollection <ToolButtonViewModel>(); Buttons.Add(new ToolButtonViewModel { Img = BitmapHelper.GetImg(Icons.Download), Click = () => { var page = new CreateDownloadTaskPage(); page.DataContext = new CreateDownloadTaskPageViewModel() { Self = page }; DialogHelper.ShowDialog(page); } }); }
protected override BitmapSource ReadData(Picture image, Stream stream, int length, BitmapPalette palette, PixelFormat?pixelFormat) { int bytesPerPixel = _pixelFormat.BitsPerPixel / 8; int stride = BitmapHelper.Stride(image.Width, _pixelFormat.BitsPerPixel); int paddingBytes = stride - image.Width * bytesPerPixel; MemoryStream stridedStream = new MemoryStream(); for (int pos = 0; pos < length; pos += image.Width * bytesPerPixel) { stream.CopyBytesTo(stridedStream, image.Width * bytesPerPixel); stridedStream.Position += paddingBytes; } byte[] bytes = stridedStream.ToArray(); return(BitmapSource.Create(image.Width, image.Height, 96, 96, _pixelFormat, palette, bytes, stride)); }
private Thumbnail ProcessThumbnail(TempUploadedFile file) { Thumbnail thumb = null; try { Stream ms = new MemoryStream(); string thumbPublicUrl = string.Empty; if (this.serviceLocalStorage.DownloadFile(ref ms, file.Path, _container)) { var picture = serviceWaveform.GetWaveform(file.Path, ms); byte[] pictureasByteArray = BitmapHelper.ImageToByte2(picture); thumbPublicUrl = this.serviceLocalStorage.UploadFile(pictureasByteArray, file.Path, "projects"); } ms.Close(); string internalName = GenerateThumbnailName(file); string path = GenerateThumbnailPath(file); thumb = new Thumbnail() { AuthCredentialId = file.AuthCredentialId, DisplayName = file.Name, InternalName = internalName, Path = path, PublicUrl = thumbPublicUrl//dropboxsavedFile.PublicUrl }; } catch (Exception exc) { this.serviceLog.LogError("TempFileService::Process-Thumbnail", exc); thumb = new Thumbnail() { PublicUrl = "/content/images/thumbnail_error.png" }; } this.repositoryThumbnail.Create(thumb); return(thumb); }
private Bitmap RenderImage() { Bitmap bitmap = TokenControl.RenderVisual(TokenControl.ZoomControl); //Apply the special filter. BitmapHelper.AddFilter(bitmap, _vm.SpecialFilterColor, _specialFilter); // Modify the rendered image. if (_overlayInfo != null) { var overlay = ImageLoader.LoadBitmapFromFile(_overlayInfo.Info.File, TokenControl.ImageResolutionWidth, TokenControl.ImageResolutionHeight); BitmapHelper.AddFilter(overlay, _overlayTintColor, _overlayBlendFilter); if (!string.IsNullOrWhiteSpace(_overlayInfo.Info.Mask)) { BitmapHelper.ApplyMaskToImage(overlay, ImageLoader.LoadBitmapFromFile(_overlayInfo.Info.Mask, TokenControl.ImageResolutionWidth, TokenControl.ImageResolutionHeight)); } BitmapHelper.AddLayerToImage(bitmap, overlay); } if (_borderInfo != null) { if (!string.IsNullOrWhiteSpace(_borderInfo.Info.Mask)) { BitmapHelper.ApplyMaskToImage(bitmap, ImageLoader.LoadBitmapFromFile(_borderInfo.Info.Mask, TokenControl.ImageResolutionWidth, TokenControl.ImageResolutionHeight)); } var border = ImageLoader.LoadBitmapFromFile(_borderInfo.Info.File, TokenControl.ImageResolutionWidth, TokenControl.ImageResolutionHeight); BitmapHelper.AddFilter(border, _borderTintColor, _borderBlendFilter); BitmapHelper.AddLayerToImage(bitmap, border); //Draw the border } if (TokenControl.ZoomControl_Text.Visibility == Visibility.Visible) { Bitmap text = TokenControl.RenderVisual(TokenControl.ZoomControl_Text); BitmapHelper.AddLayerToImage(bitmap, text); } if (_vm.Autocrop) { bitmap = BitmapHelper.Autocrop(bitmap); } //Since we just spent time rendering the image, we might as well update the preview even if the user didn't ask for that specifically. _preWindow?.SetImage(bitmap); return(bitmap); }
internal void UpdateOverlays() { //When updating overlays, the actual output resolution may be different from the desired resolution if stretching of overlays isn't allowed. if (_borderInfo != null) { var border = ImageLoader.LoadBitmapFromFile(_borderInfo.Info.File); TokenControl.SetDimensions(border.Width, border.Height); } else if (_overlayInfo != null) { var overlay = ImageLoader.LoadBitmapFromFile(_overlayInfo.Info.File); TokenControl.SetDimensions(overlay.Width, overlay.Height); } if (_borderInfo != null) { //Border var borderImage = ImageLoader.LoadBitmapFromFile(_borderInfo.Info.File, TokenControl.ImageResolutionWidth, TokenControl.ImageResolutionHeight); BitmapHelper.AddFilter(borderImage, _borderTintColor, _borderBlendFilter); TokenControl.SetBorderImage(borderImage); TokenControl.BorderImage.Height = TokenControl.ImageResolutionHeight; TokenControl.BorderImage.Width = TokenControl.ImageResolutionWidth; } if (_overlayInfo != null) { //Overlay var overlayImage = ImageLoader.LoadBitmapFromFile(_overlayInfo.Info.File, TokenControl.ImageResolutionWidth, TokenControl.ImageResolutionHeight); BitmapHelper.AddFilter(overlayImage, _overlayTintColor, _overlayBlendFilter); if (!string.IsNullOrWhiteSpace(_overlayInfo.Info.Mask)) { BitmapHelper.ApplyMaskToImage(overlayImage, ImageLoader.LoadBitmapFromFile(_overlayInfo.Info.Mask, TokenControl.ImageResolutionWidth, TokenControl.ImageResolutionHeight)); } TokenControl.SetOverlayImage(overlayImage); TokenControl.OverlayImage.Height = TokenControl.ImageResolutionHeight; TokenControl.OverlayImage.Width = TokenControl.ImageResolutionWidth; } if (SettingsManager.AutoUpdatePreview) { RenderUsingDispatcher(); } }
/// <summary> /// Attempts three times to download an image from the provided URL. /// </summary> public Bitmap DownloadImage(string url) { // Sanity check if (url == null || url == "") { return(null); } int attempts = 0; while (attempts <= 3) { attempts++; try { byte[] byteArray = GetRemoteImage(url); if (byteArray == null) { // No bytes returned. } else { Bitmap bitmap = BitmapHelper.GetBitmapFromByteArray(byteArray); if (bitmap.Height > MAX_STORAGE_HEIGHT || bitmap.Width > MAX_STORAGE_WIDTH) { Bitmap sizedBitmap = BitmapHelper.GetSizedBitmap(bitmap, MAX_STORAGE_WIDTH, MAX_STORAGE_HEIGHT); bitmap.Dispose(); bitmap = null; return(sizedBitmap); } return(bitmap); } } catch { // :S Do we throw or ignore? } } return(null); }
public void ImageDataToBitmapSourceTest() { var imagedata = File.ReadAllBytes("testnotimage.jpg"); var bitmap_notimage = BitmapHelper.ImageDataToBitmap(imagedata); Assert.IsNull(bitmap_notimage); imagedata = File.ReadAllBytes("testjpg.jpg"); var bitmap_jpg = BitmapHelper.ImageDataToBitmap(imagedata); Assert.IsNotNull(bitmap_jpg); Assert.AreEqual(bitmap_jpg.PixelFormat, PixelFormat.Format24bppRgb); bitmap_jpg.Save("testjpg_.jpg"); imagedata = File.ReadAllBytes("testjpg8.jpg"); var bitmap8_jpg = BitmapHelper.ImageDataToBitmap(imagedata); Assert.IsNotNull(bitmap8_jpg); Assert.AreEqual(bitmap8_jpg.PixelFormat, PixelFormat.Format24bppRgb); bitmap8_jpg.Save("testjpg8_.jpg"); imagedata = File.ReadAllBytes("testpng.png"); var bitmap_png = BitmapHelper.ImageDataToBitmap(imagedata); Assert.IsNotNull(bitmap_png); bitmap_png.Save("testpng_.jpg"); imagedata = File.ReadAllBytes("testwebp.webp"); var bitmap_webp = BitmapHelper.ImageDataToBitmap(imagedata); Assert.IsNotNull(bitmap_webp); bitmap_webp.Save("testwebp_.jpg"); imagedata = File.ReadAllBytes("testheic.heic"); var bitmap_heic = BitmapHelper.ImageDataToBitmap(imagedata); Assert.IsNotNull(bitmap_heic); bitmap_webp.Save("testheic_.jpg"); imagedata = File.ReadAllBytes("testgif.gif"); var bitmap_gif = BitmapHelper.ImageDataToBitmap(imagedata); Assert.IsNotNull(bitmap_gif); bitmap_gif.Save("testgif_.jpg"); }
public void GetBrightnessTest() { var lblack = BitmapHelper.GetBrightness(0, 0, 0); Assert.IsTrue(Math.Abs(lblack - 0.00) < 0.01); var lred = BitmapHelper.GetBrightness(255, 0, 0); Assert.IsTrue(Math.Abs(lred - 53.23) < 0.01); var lgreen = BitmapHelper.GetBrightness(0, 255, 0); Assert.IsTrue(Math.Abs(lgreen - 87.74) < 0.01); var lblue = BitmapHelper.GetBrightness(0, 0, 255); Assert.IsTrue(Math.Abs(lblue - 32.30) < 0.01); var lwhite = BitmapHelper.GetBrightness(255, 255, 255); Assert.IsTrue(Math.Abs(lwhite - 100.00) < 0.01); }
private Bitmap LoadImage() { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Selecione uma image."; ofd.Filter = "Arquivo de imagens|*.bmp;*.jpg;*.jpeg;*.png"; var result = ofd.ShowDialog(); if (result.HasValue && result.Value) { StreamReader streamReader = new StreamReader(ofd.FileName); var image = (Bitmap)Bitmap.FromStream(streamReader.BaseStream); streamReader.Close(); return(BitmapHelper.Fix(image)); } return(null); }