Skip to main content
 首页 » 编程设计

c#之无法调整已更改图像的大小 C# 窗体

2024年02月23日18yjmyzz

如果我更改图像的颜色然后尝试调整大小,它只会调整原始图像的大小。为什么会发生这种情况,我该如何解决?

这是我的代码:

private PrintDocument printDoc = new PrintDocument(); 
private PageSettings pgSettings = new PageSettings(); 
private PrinterSettings prtSettings = new PrinterSettings(); 
 
Bitmap myBitmapImage;      // image (bitmap) for some background mountains 
Boolean isInvert = false; 
Boolean isLOaded = false; 
Boolean isGrayscale = false; 
Boolean isThreshold = false; 
Boolean isResize = false; 
 
OpenFileDialog ofd; 
Bitmap bmBack;  
 
public EditImage() 
{ 
    printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);  
 
    InitializeComponent(); 
    this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true); 
} 
 
private void EditImage_Load(object sender, EventArgs e) 
{ 
 
} 
 
private void EditImage_Paint(object sender, PaintEventArgs e) 
{ 
    if (isLOaded == true) 
    { 
        Graphics gWindow;   // reference to the graphic surface of this window 
        Graphics gBack;     // reference to in-memory surface 
 
        bmBack = new Bitmap(Width, Height);     // bitmap for window surface copy 
 
        gWindow = e.Graphics;   // get our current window's surface 
        gBack = Graphics.FromImage(bmBack);     // create surfaces from the bitmaps 
 
        gBack.DrawImage(myBitmapImage, 0, 0, Width, Height); 
 
        if (isInvert == true) 
        { 
            InvertBitmap(bmBack); 
        } 
        else if (isGrayscale == true) 
        { 
            GrayscaleBitmap(bmBack); 
        } 
        else if (isThreshold == true) 
        { 
            ThresholdBitmap(bmBack); 
        } 
        else if (isResize == true) 
        { 
            bmBack = resizeImage(bmBack, 10, 100); 
        } 
 
        gWindow.DrawImage(bmBack, 0, 0); 
    } 
} 
private void toolStripMenuItemLoadImage_Click(object sender, EventArgs e) 
{ 
    using (ofd = new OpenFileDialog()) 
    { 
        ofd.Title = "Load Image"; 
 
        if (ofd.ShowDialog() == DialogResult.OK) 
        { 
            myBitmapImage = new Bitmap(ofd.FileName); 
            this.Invalidate();  
        } 
    } 
    isLOaded = true; 
} 
 
 
private void GrayscaleBitmap(Bitmap bmp) 
{ 
    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); 
    System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, 
        System.Drawing.Imaging.PixelFormat.Format32bppRgb); 
 
    IntPtr ptr = bmpData.Scan0; 
 
    int numPixels = bmpData.Width * bmp.Height; 
    int numBytes = numPixels * 4; 
    byte[] rgbValues = new byte[numBytes]; 
 
    System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, numBytes); 
 
    for (int i = 0; i < rgbValues.Length; i += 4) 
    { 
        byte gray = (byte)(.3 * rgbValues[i + 0]); //blue 
        gray += (byte)(.59 * rgbValues[i + 1]); //green 
        gray += (byte)(.11 * rgbValues[i + 2]); //red 
 
        rgbValues[i + 0] = gray; //blue 
        rgbValues[i + 1] = gray; //green 
        rgbValues[i + 2] = gray; //red 
    } 
 
    System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, numBytes); 
 
    bmp.UnlockBits(bmpData); 
} 
 
private  Bitmap resizeImage(Bitmap sourceBMP, int width, int height) 
{ 
    Bitmap result = new Bitmap(width, height); 
    using(Graphics g = Graphics.FromImage(result)) 
    g.DrawImage(sourceBMP, 0, 0, width, height); 
 
    return result; 
} 

我还有处理用户单击按钮并将 bool 值设置为适当值的方法,以便它调用正确的方法。图像确实会改变颜色 - 正如预期的那样......但是当我点击调整大小时,我希望它调整已经改变颜色的图像版本 - 而不是原始图像......

请您参考如下方法:

这里有很多错误:

resizeImage()

  • 您应该释放在这里创建的 Graphics 对象(将其包装在 using() 语句中)
  • 不要从这里调用 invalidate,这个函数只是调整你的图像大小,你在改变图像后失效,你的 Paint 方法绘制现在已经改变的图像。
  • 如果您不再需要它,您还应该考虑在返回函数之前立即处理 sourceBMP。

灰度位图()

  • 这看起来是对的,但同样没有理由在这里无效。在调用函数中调用此方法后,您应该使它失效。这更有意义。

EditImage_Paint

  • 您不应在 Paint 事件中调用这些函数。并且您不应该在每个 Paint 上创建一个新的位图和一个新的图形类。这比必要的工作要多得多。只有在需要根据用户输入更改数据时(用户单击按钮应用灰度效果),才应执行这些函数。

对于你想做的事情,你最多只需要2个位图变量。一个存储原始未修改的位图,以防您想让用户“重置”它或撤消任何效果(大多数效果会导致永久性数据丢失,您无法再次使灰度图像变色)。另一个存储绘制的位图。每次用户应用效果时,它都会修改第二个位图,然后调用 invalidate。

您的 Paint 函数应该做的就是绘制第二个位图:

Bitmap originalBitmap; // load from file, do not modify 
Bitmap currentBitmap; // when user clicks an effect, modify this bitmap and then Invalidate afterward 
 
private void EditImage_Paint(object sender, PaintEventArgs e) 
{ 
    e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
 
    // Draw the currentBitmap centered on the window 
    SizeF clientSize = this.ClientSize; 
 
    double zoomRatio = Math.Min( 
        clientSize.Width / currentBitmap.Width, 
        clientSize.Height / currentBitmap.Height 
    ); 
 
    SizeF zoomedSize = new SizeF( 
        (float)(currentBitmap.Width * zoomRatio), 
        (float)(currentBitmap.Height * zoomRatio) 
    ); 
 
    PointF imageOffset = new PointF( 
        (clientSize.Width - zoomedSize.Width) / 2, 
        (clientSize.Height - zoomedSize.Height) / 2 
    ); 
 
    e.Graphics.Clear(Color.White); 
    e.Graphics.DrawImage(currentBitmap, imageOffset.X, imageOffset.Y, zoomedSize.Width, zoomedSize.Height); 
} 

这模拟了一个简单的缩放效果,将图像置于控件的中心并绘制它以适合窗口。