c# – 在图形变换比例后缺少第一个像素列的一半

前端之家收集整理的这篇文章主要介绍了c# – 在图形变换比例后缺少第一个像素列的一半前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我注意到在OnPaint事件上的图形转换比例之后,不会绘制图像的第一个像素列的一半.

重现它所需的所有代码都在帖子的末尾.基本上我已经创建了一个派生自PictureBox的类,名为PictureBox2,它会覆盖OnPaint方法来执行Scale转换.它还将InterpolationMode更改为NearestNeighbor以防止Graphics更改像素外观.

PictureBox控件已添加到名为Form6_GraphicsTest的Form中.控制在所有方面都是固定的. PictureBox2背面颜色更改为蓝色,Form返回颜色为深灰色.

正如您在下面的图像中看到的那样,只绘制了图像第一个像素列的1/2.为什么??我在这里遗漏了什么?

这是原始的10×10 8bpp图像:

编辑 – 解决方
对于一些奇怪的原因,PixelOffsetMode.Default吃了0.5像素.解决方案:PixelOffsetMode.Half或HighQuality!

代码PictureBox2.cs

  1. using System.Drawing;
  2. using System.Drawing.Drawing2D;
  3. using System.Windows.Forms;
  4.  
  5. namespace GraphicsTest
  6. {
  7. public class PictureBox2 : PictureBox
  8. {
  9. public PointF Zoom = new PointF(20,20);
  10. private InterpolationMode interpolationMode = InterpolationMode.NearestNeighbor;
  11.  
  12. /// <summary>
  13. /// Paint the image
  14. /// </summary>
  15. /// <param name="e">The paint event</param>
  16. protected override void OnPaint(PaintEventArgs e)
  17. {
  18. if (IsDisposed)
  19. return;
  20.  
  21. if (Image != null)
  22. {
  23. if (e.Graphics.InterpolationMode != interpolationMode)
  24. e.Graphics.InterpolationMode = interpolationMode;
  25.  
  26. using (Matrix transform = e.Graphics.Transform)
  27. {
  28. //e.Graphics.ResetTransform();
  29.  
  30. if (Zoom.X != 1.0 || Zoom.Y != 1.0)
  31. transform.Scale(Zoom.X,Zoom.Y,MatrixOrder.Append);
  32.  
  33. //if (ImageDisplayLocation.X != 0 || ImageDisplayLocation.Y != 0) //Convert translation back to display pixel unit.
  34. // transform.Translate(ImageDisplayLocation.X / Zoom.X,ImageDisplayLocation.Y / Zoom.Y);
  35.  
  36. e.Graphics.Transform = transform;
  37. }
  38. }
  39.  
  40. base.OnPaint(e);
  41.  
  42. //If you want to draw something over the control in control coordinate,you must first reset the transformation! :D
  43. //e.Graphics.ResetTransform();
  44. //Draw your stuff
  45. }
  46. }
  47. }

代码Form6_GraphicsTest.cs:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace GraphicsTest
  11. {
  12. public class Form6_GraphicsTest : Form
  13. {
  14. public Form6_GraphicsTest()
  15. {
  16. InitializeComponent();
  17. Bitmap bmp = new Bitmap(@"D:\Test 10x10.8bpp.png");
  18. this.pictureBox21.Image = bmp;
  19.  
  20. this.pictureBox21.Zoom = new PointF(20,20);
  21.  
  22. this.ClientSize = new Size(Convert.ToInt32(this.pictureBox21.Zoom.X * bmp.Width) + 30,Convert.ToInt32(this.pictureBox21.Zoom.Y * bmp.Height) + 30);
  23. }
  24.  
  25. /// <summary>
  26. /// required designer variable.
  27. /// </summary>
  28. private System.ComponentModel.IContainer components = null;
  29.  
  30. /// <summary>
  31. /// Clean up any resources being used.
  32. /// </summary>
  33. /// <param name="disposing">true if managed resources should be disposed; otherwise,false.</param>
  34. protected override void Dispose(bool disposing)
  35. {
  36. if (disposing && (components != null))
  37. {
  38. components.Dispose();
  39. }
  40. base.Dispose(disposing);
  41. }
  42.  
  43. #region Windows Form Designer generated code
  44.  
  45. /// <summary>
  46. /// required method for Designer support - do not modify
  47. /// the contents of this method with the code editor.
  48. /// </summary>
  49. private void InitializeComponent()
  50. {
  51. this.pictureBox21 = new GraphicsTest.PictureBox2();
  52. ((System.ComponentModel.ISupportInitialize)(this.pictureBox21)).BeginInit();
  53. this.SuspendLayout();
  54. //
  55. // pictureBox21
  56. //
  57. this.pictureBox21.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  58. | System.Windows.Forms.AnchorStyles.Left)
  59. | System.Windows.Forms.AnchorStyles.Right)));
  60. this.pictureBox21.BackColor = System.Drawing.SystemColors.Highlight;
  61. this.pictureBox21.Location = new System.Drawing.Point(12,12);
  62. this.pictureBox21.Name = "pictureBox21";
  63. this.pictureBox21.Size = new System.Drawing.Size(260,238);
  64. this.pictureBox21.TabIndex = 0;
  65. this.pictureBox21.TabStop = false;
  66. //
  67. // Form6_GraphicsTest
  68. //
  69. this.AutoScaleDimensions = new System.Drawing.SizeF(6F,13F);
  70. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  71. this.BackColor = System.Drawing.SystemColors.ControlDarkDark;
  72. this.ClientSize = new System.Drawing.Size(284,262);
  73. this.Controls.Add(this.pictureBox21);
  74. this.Name = "Form6_GraphicsTest";
  75. this.Text = "Form6_GraphicsTest";
  76. ((System.ComponentModel.ISupportInitialize)(this.pictureBox21)).EndInit();
  77. this.ResumeLayout(false);
  78.  
  79. }
  80.  
  81. #endregion
  82.  
  83. private PictureBox2 pictureBox21;
  84. }
  85. }
@H_403_22@解决方法
它可能与 PixelOffsetMode有关吗?这可能与其他 post有关.它与渲染效率有关…

猜你在找的C#相关文章