Exemple #1
0
 private void PaintBox_MouseMove(object sender, MouseEventArgs e)
 {
     if (IsMouseDwon == true)  //  if mouse is clicked///
     {
         if (LastPoint != null)
         {
             g.DrawLine(pencel, LastPoint, Point_Initialization(Img, e.X, e.Y));
             g.SmoothingMode = SmoothingMode.AntiAlias;
             PaintBox.Invalidate();                           //  refresh picturebox//
             LastPoint = Point_Initialization(Img, e.X, e.Y); //update last point position//
         }
     }
 }
        private void EditImageFileRightButtonClick(object sender, EventArgs e)
        {
            string newFilename;
            bool   usePngFile;
            Bitmap uIImage;
            Bitmap bitmap;
            int    size;

            OpenFileDialog openDialog = new OpenFileDialog();

            openDialog.Filter           = "BMP and PNG files|*.bmp;*.png|BMP files|*.bmp|PNG files|*.png";
            openDialog.CheckFileExists  = true;
            openDialog.ReadOnlyChecked  = false;
            openDialog.Title            = "Open Image File";
            openDialog.InitialDirectory = Path.GetDirectoryName(_filename);
            openDialog.FileName         = Path.GetFileName(_filename);
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                bool saveToBmp = false;
                newFilename = openDialog.FileName;

                usePngFile = Settings.Instance.AllowPngImages && Path.GetExtension(openDialog.FileName).Equals(".png", StringComparison.OrdinalIgnoreCase);

                //UIImage will automatically convert to 32 - bit alpha image

                bitmap  = null;
                uIImage = AlphaBitmap.TryAlphaBitmapFromFile(newFilename, (_flags & ImageFlags.HighContrast) != 0);

                try
                {
                    bitmap = uIImage.Clone(new Rectangle(new Point(), uIImage.Size), uIImage.PixelFormat);
                    //bitmap = Bitmap.FromHbitmap(uIImage.GetHbitmap()); // this code do not work, because it will not produce a ARGB bitmap

                    if (!Addons.StartsText(_image.Owner.Directory, newFilename))
                    {
                        newFilename = Path.Combine(_image.Owner.Directory, "Res");
                        Addons.ForceDirectories(newFilename);
                        newFilename = Path.Combine(newFilename, Path.GetFileName(openDialog.FileName));

                        if (usePngFile)
                        {
                            File.Copy(openDialog.FileName, newFilename, true);
                        }
                        else
                        {
                            saveToBmp = true;
                        }

                        //newFilename = Path.ChangeExtension(newFilename, ".bmp");
                        //bitmap.Save(newFilename, ImageFormat.Bmp); //@ changed, don't override the same file
                        //It seems to be a better solution when we copy the file when it is a .bmp with BM Header than bitmap.Save
                        //If the file from openDialog is the same as newFilename we can delete the openDialog file first
                        //or jump around the bitmap.Save
                        //we have to do some checks if the choosen file has a BM Header
                        //or is it possible for the WindowsRibbon, that we can use png files ?
                    }
                    if (!usePngFile && !Path.GetExtension(newFilename).Equals(".bmp", StringComparison.OrdinalIgnoreCase))
                    {
                        saveToBmp   = true;
                        newFilename = Path.ChangeExtension(newFilename, ".bmp");
                    }
                    if (saveToBmp)
                    {
                        AlphaBitmap.SetTransparentRGB(bitmap, Color.LightGray.ToArgb() & 0xffffff);
                        bitmap.Save(newFilename, ImageFormat.Bmp); //@ changed, don't override the same file
                    }

                    EditImageFile.Text = _image.Owner.BuildRelativeFilename(newFilename);
                    _image.Source      = EditImageFile.Text;

                    ClearBitmap(_bitmap);
                    Graphics canvas = Graphics.FromImage(_bitmap);
                    if (uIImage.PixelFormat != PixelFormat.Format32bppArgb)
                    {
                        uIImage.MakeTransparent();
                    }
                    canvas.DrawImage(uIImage, new Point((64 - uIImage.Width) / 2, (64 - uIImage.Height) / 2));
                    canvas.Dispose();
                    PaintBox.Invalidate();

                    size = Math.Max(bitmap.Width, bitmap.Height);
                    if ((_flags & ImageFlags.Large) != 0)
                    {
                        size = size / 2;
                    }
                    if (size <= 16)
                    {
                        ComboBoxMinDpi.SelectedIndex = 0;
                    }
                    else if (size <= 20)
                    {
                        ComboBoxMinDpi.SelectedIndex = 2;
                    }
                    else if (size <= 24)
                    {
                        ComboBoxMinDpi.SelectedIndex = 3;
                    }
                    else
                    {
                        ComboBoxMinDpi.SelectedIndex = 4;
                    }
                }
                finally
                {
                    uIImage.Dispose();
                    bitmap.Dispose();
                }
            }
        }