创建小位图作为现有的一部分 [英] Creating small bitmap as a part of existing

查看:75
本文介绍了创建小位图作为现有的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建并显示现有位图的一部分。鼠标单击图片框会显示一个复制到新位图的小矩形。问题是新的位图具有不可预测的偏移而不是完全显示矩形的内容。我想问题是我使用GraphicsUnit.Point而不是Pixel格式,但在尝试使用Pixel格式时,我只看到白色矩形而不是图片。

< br $>




//点击源图像的图片框

private void pictureBox1_Click(object sender,EventArgs e)

{

int x =((MouseEventArgs)e).X;

int y =((MouseEventArgs)e).Y;



m_Rectangle = new Rectangle(x,y,20,20);

pictureBox1.Refresh();

m_Bitmap = CopyToBitmap();

}



//用户点击后显示红色矩形

priva te void pictureBox1_Paint(object sender,PaintEventArgs e)

{

if(m_Rectangle!= null)

{

var g = e.Graphics;

g.DrawRectangle(Pens.Red,m_Rectangle);



}

}



私人位图CopyToBitmap()

{



位图source =(Bitmap)pictureBox1.Image;



Rectangle section = new Rectangle(new Point(m_Rectangle.Location.X,m_Rectangle.Location.Y),new Size( 20,20));



位图bmp =新位图(section.Width,section.Height,PixelFormat.Format32bppArgb);





使用(Graphics g = Graphics.FromImage(bmp))

{

g.DrawImage(source, section,m_Rectangle,Grap hicsUnit.Point);

}

// picLetter =目的地图片框



picLetter.Image = bmp ;

picLetter.Refresh();

返回bmp;



}



我尝试了什么:



我使用C#WinForms,.Net Framework 3.5。 X位置的偏移是不一致的并且图片框没有准确显示红色矩形的内容

I"m trying to create and show a part of existing bitmap. The mouse click over the picture box shows a small rectangle that is copied to a new bitmap. The problem is that a new bitmap has an unpredictable offset and does not show the content of the rectangle exactly. I guess the problem is that I use GraphicsUnit.Point instead of Pixel format, but while trying using the Pixel format I see just white rectangle instead of the picture.



//Click on picturebox of source image
private void pictureBox1_Click(object sender, EventArgs e)
{
int x = ((MouseEventArgs)e).X;
int y = ((MouseEventArgs)e).Y;

m_Rectangle = new Rectangle(x, y, 20, 20);
pictureBox1.Refresh();
m_Bitmap = CopyToBitmap();
}

//Display red rectangle after user click
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (m_Rectangle != null)
{
var g = e.Graphics;
g.DrawRectangle(Pens.Red, m_Rectangle);

}
}

private Bitmap CopyToBitmap()
{

Bitmap source = (Bitmap)pictureBox1.Image;

Rectangle section = new Rectangle(new Point(m_Rectangle.Location.X ,m_Rectangle.Location.Y), new Size(20, 20));

Bitmap bmp = new Bitmap( section.Width, section.Height, PixelFormat.Format32bppArgb);


using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(source, section, m_Rectangle,GraphicsUnit.Point);
}
//picLetter = Destination picture box

picLetter.Image = bmp;
picLetter.Refresh();
return bmp;

}

What I have tried:

I use C# WinForms, .Net Framework 3.5. The offset of X position is unconsistent and the picturebox does not show exactly the content of red rectangle

推荐答案

问题恰恰是:点不是像素。

您的矩形以像素为单位测量 - 因为这是鼠标移动的原因。
但是您的图像绘制在Points中,这是一个基于打印的系统,其中一个Point为1 / 72英寸 - 这两个系统甚至不兼容,因为您不知道特定显示器上的像素和点之间的关系是什么!



And Point结构与Point图形单元无关!



直到你将图像更改为像素(或找到从鼠标坐标到点的映射)代码将不工作。



试试这个:

The problem is exactly that: Points are not Pixels.
Your rectangle is measured in Pixels - because that is what mouse movements are in.
But ]your image drawing is in Points, which are a print based system where one Point is 1/72th of an inch - the two systems aren;t even slightly compatible, because you have no idea what the relationship between Pixels and Points on your specific monitor is!

And the Point struct has nothing to do with Point the graphics unit!

Until you change your image to Pixels (or find a mapping from mouse coordinates to Points) that code will not work.

Try this:
private void myPictureBox_Click(object sender, EventArgs e)
    {
    int x = ((MouseEventArgs)e).X;
    int y = ((MouseEventArgs)e).Y;
    Rectangle m_Rectangle = new Rectangle(x, y, 100, 100);
    Bitmap source = (Bitmap)myPictureBox.Image;
    Rectangle section = new Rectangle(new Point(x, y), new Size(100, 100));
    Bitmap bmp = new Bitmap(section.Width, section.Height, PixelFormat.Format32bppArgb);
    using (Graphics g = Graphics.FromImage(bmp))
        {
        g.DrawImage(source, new Rectangle(0, 0, pbShowSection.Width, pbShowSection.Height), m_Rectangle, GraphicsUnit.Pixel);
        }
    pbShowSection.Image = bmp;
    }


这篇关于创建小位图作为现有的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆