/// <summary> /// This method is used to load a PixelDatabase and its DirectBitmap object from an imagePath /// </summary> /// <param name="imagePath">The path to the image</param> /// <param name="updateCallback">The delegate to call during load for status.</param> /// <param name="copyInPlaceForResetImage">If true, a copy of the image will be created, and the ResetPath and UndoPath will be set to this new filename.</param> /// <returns></returns> public static PixelDatabase LoadPixelDatabase(string imagePath, StatusUpdate updateCallback) { // initial valule PixelDatabase pixelDatabase = null; try { // if we have an imagePath if ((TextHelper.Exists(imagePath)) && (File.Exists(imagePath))) { // create the Bitmap using (Bitmap bitmap = (Bitmap)Bitmap.FromFile(imagePath)) { // load the pixelDatabase pixelDatabase = LoadPixelDatabase(bitmap, updateCallback); } } } catch (Exception error) { // write to console for now DebugHelper.WriteDebugError("LoadPixelDatabase", "PixelDatabaseLoader", error); } finally { } // return value return(pixelDatabase); }
/// <summary> /// This method is used to load a PixelDatabase and its DirectBitmap object. /// </summary> /// <param name="bitmap"></param> /// <returns></returns> public static PixelDatabase LoadPixelDatabase(Image original, StatusUpdate updateCallback) { // initial valule PixelDatabase pixelDatabase = null; try { // convert to a bitmap Bitmap bitmap = (Bitmap)original; // Load the PixelDatabase pixelDatabase = LoadPixelDatabase(bitmap, updateCallback); } catch (Exception error) { // write to console for now DebugHelper.WriteDebugError("LoadPixelDatabase", "PixelDatabaseLoader", error); } finally { } // return value return(pixelDatabase); }
/// <summary> /// event is fired when the 'Canvas' is clicked. /// </summary> private void Canvas_Click(object sender, EventArgs e) { // locals int x; int y; // If the value for the property this.ColorPickerMode is true or RectangleMode is true if ((ColorPickerMode) || (RectangleMode)) { // get the mouse event args MouseEventArgs mouseEventArgs = e as MouseEventArgs; // If the mouseEventArgs object exists if (mouseEventArgs != null) { // Get the x & y x = mouseEventArgs.X; y = mouseEventArgs.Y; if (ColorPickerMode) { // Handle the PixelInfo HandlePixelInfo(x, y); } else if (RectangleMode) { // set the pixel PixelInformation pixel = null; if (Point1 == Point.Empty) { // Get the scaled Pixel pixel = HandlePixelInfo(x, y); // Create Point1 Point1 = new Point(pixel.X, pixel.Y); } else if (Point2 == Point.Empty) { // Get the scaled Pixel pixel = HandlePixelInfo(x, y); // Create Point1 Point2 = new Point(pixel.X, pixel.Y); // Draw a rectangle PixelDatabase.DrawRectangle(Point1, Point2); } } } } }
/// <summary> /// method returns the Click Handler /// </summary> public void ButtonClickHandler(int buttonNumber, string buttonName) { // Hide this PixelInfo.Visible = false; // Determine the action by the buttonNumber switch (buttonNumber) { case 1: // Close the current file CloseFile(); // required break; case 2: // Save the current file Save(); // required break; case 3: // Save the current file SaveAs(); // required break; case 4: // Reset Image Reset(); // required break; case 5: // Undo the last change UndoChanges(); // required break; case 6: // Set ColorPickerMode true or false this.ColorPickerMode = !this.ColorPickerMode; // We can't be in both at the same time this.RectangleMode = !ColorPickerMode; // if not in ColorPickerMode if (this.ColorPickerMode) { // it is on MessagesLabel.Text = "Color Picker Mode On"; // Hide the control this.PixelInfo.Visible = false; } else { // it is on MessagesLabel.Text = "Color Picker Mode Off"; // Hide the control this.PixelInfo.Visible = false; } // required break; case 7: // Apply Query // Parse and apply the current query PixelQuery pixelQuery = PixelDatabase.ApplyQuery(QueryTextBox.Text, this.StatusUpdate); // Set the Bitmap now that is has been updated Canvas.BackgroundImage = PixelDatabase.DirectBitmap.Bitmap; // required break; case 8: // Parse and apply the current query this.Abort = true; // required break; } // Refresh everything Refresh(); Application.DoEvents(); }
/// <summary> /// This method is used to load a PixelDatabase and its DirectBitmap object. /// </summary> /// <param name="bitmap"></param> /// <returns></returns> public static PixelDatabase LoadPixelDatabase(Bitmap original, StatusUpdate updateCallback) { // initial valule PixelDatabase pixelDatabase = null; // locals int max = 0; try { // if we have an image if (NullHelper.Exists(original)) { // create a new bitmap using (Bitmap source = new Bitmap(original)) { // Create a new instance of a 'PixelDatabase' object. pixelDatabase = new PixelDatabase(); // Create a DirectBitmap pixelDatabase.DirectBitmap = new DirectBitmap(source.Width, source.Height); // Code To Lockbits BitmapData bitmapData = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadWrite, source.PixelFormat); IntPtr pointer = bitmapData.Scan0; int size = Math.Abs(bitmapData.Stride) * source.Height; byte[] pixels = new byte[size]; Marshal.Copy(pointer, pixels, 0, size); // End Code To Lockbits // Marshal.Copy(pixels,0,pointer, size); source.UnlockBits(bitmapData); // locals Color color = Color.FromArgb(0, 0, 0); int red = 0; int green = 0; int blue = 0; int alpha = 0; // variables to hold height and width int width = source.Width; int height = source.Height; int x = -1; int y = 0; // if the UpdateCallback exists if (NullHelper.Exists(updateCallback)) { // Set the value for max max = height * width; // Set the graph max updateCallback("SetGraphMax", max); } // Iterating the pixel array, every 4th byte is a new pixel, much faster than GetPixel for (int a = 0; a < pixels.Length; a = a + 4) { // increment the value for x x++; // every new column if (x >= width) { // reset x x = 0; // Increment the value for y y++; } // get the values for r, g, and blue blue = pixels[a]; green = pixels[a + 1]; red = pixels[a + 2]; alpha = pixels[a + 3]; // create a color color = Color.FromArgb(alpha, red, green, blue); // Set the pixel at this spot pixelDatabase.DirectBitmap.SetPixel(x, y, color); } } // Create the MaskManager pixelDatabase.MaskManager = new MaskManager(); } } catch (Exception error) { // write to console for now DebugHelper.WriteDebugError("LoadPixelDatabase", "PixelDatabaseLoader", error); } // return value return(pixelDatabase); }
/// <summary> /// event is fired when the 'RandomizeButton' is clicked. /// </summary> private void RandomizeButton_Click(object sender, EventArgs e) { // setup our images string sliceImage = SliceImageControl.Text; string targetImage = TargetImageControl.Text; int rowHeight = 0; int rowWidth = 0; bool isValid = false; int offSetX = 0; int offSetY = 0; int increment = 0; int incrementedValue = 0; // load the target database PixelDatabase targetDatabase = PixelDatabaseLoader.LoadPixelDatabase(targetImage, this.Callback); // verify both objects exist if (NullHelper.Exists(sliceDatabase, targetDatabase)) { // Set the height and width rowHeight = sliceDatabase.Height; rowWidth = sliceDatabase.Width; int min = MinValueControl.IntValue; int max = MaxValueControl.IntValue; int startX = StartXControl.IntValue; int startY = StartYControl.IntValue; increment = IncrementControl.IntValue; int indent = 0; LargeNumberShuffler shuffler = null; // verify everything is in rage if ((min > 0) && (max > 0) && (max > min) || (max == 0)) { // valid isValid = true; // setup the ProgressBar this.Graph.Visible = true; this.Graph.Minimum = 0; this.Graph.Maximum = NumberSlicesControl.IntValue; // If the value for max is greater than zero if (max > 0) { // Create a new instance of a 'RandomShuffler' object. shuffler = new LargeNumberShuffler(6, 1, 375894, NumberOutOfRangeOptionEnum.ReturnModulus); } // setup the section for (int section = 0; section < NumberSlices; section++) { // Update the graph Graph.Value = section; // Add the increment to the incrementedValue incrementedValue += increment; // if max is set, else we do not need indent if (max > 0) { // pull the next item indent = (shuffler.PullNumber() % 86) + 1; } // now we need to copy the entire sourceImage onto the target, using this indent // iterate the y pixels for (int x = 0; x < rowWidth; x++) { for (int y = 0; y < rowHeight; y++) { // get this pixel PixelInformation pixel = sliceDatabase.GetPixel(x, y); // If the pixel object exists if (NullHelper.Exists(pixel)) { // get the new x offSetX = indent + startX + x; offSetY = (section * rowHeight) + y + startY + incrementedValue; if (offSetY < (targetDatabase.Height - 1)) { // Set the pixel color targetDatabase.SetPixelColor(offSetX, offSetY, pixel.Color, false, 0); } } } } } // Set the image TargetImageViewer.BackgroundImage = targetDatabase.DirectBitmap.Bitmap; // UPdate this UI Refresh(); } else { // not valid isValid = false; } } else { // not valid isValid = false; } // Show the user a message if (!isValid) { // show a message to the user MessageBox.Show("Not valid", "Invalid"); } }
/// <summary> /// event is fired when the 'CreateSubImagesButton' is clicked. /// </summary> private void CreateSubImagesButton_Click(object sender, EventArgs e) { try { // locals string imagePath = this.SourceImageControl.Text; int imageNumber = 0; int top = 0; int left = 0; string fileName = ""; Point topLeft; Rectangle size = new Rectangle(0, 0, this.WidthControl.IntValue, this.HeightControl.IntValue); PixelDatabase pixelDatabase = PixelDatabaseLoader.LoadPixelDatabase(imagePath, Refresh); int rows = RowsControl.IntValue; int columns = ColumnsControl.IntValue; Graph.Maximum = rows * columns; int height = HeightControl.IntValue; int width = WidthControl.IntValue; // Get the extension FileInfo fileInfo = new FileInfo(imagePath); // iterate the rows for (int row = 0; row < 6; row++) { // iterate the columns for (int column = 0; column < 6; column++) { // Increment the value for imageNumber imageNumber++; left = column * width; top = row * width; topLeft = new Point(left, top); size = new Rectangle(left, top, width, height); // create the subImage Image image = pixelDatabase.CreateSubImage(topLeft, size); // get the new file name without the extension string imageName = this.ImageNameControl.Text.Replace(fileInfo.Extension, ""); // set the fileName fileName = Path.Combine(this.OutputFolderControl.Text, imageName + imageNumber + fileInfo.Extension); // Save the file image.Save(fileName); Graph.Value = imageNumber; Graph.Refresh(); Application.DoEvents(); } } // Show the user a message MessageBox.Show("The sub images have been created.", "Success"); } catch (Exception error) { // show the user a message MessageBox.Show("An error occurred processing your image", "Oops. Something went wrong."); // for debugging only DebugHelper.WriteDebugError("CreateSubImagesButton_Click", this.Name, error); } }