[英]How can I place a border color in a pictureBox if the image loaded in it is from the resources?
I set my pictureBox with a default image from resources with this:我使用来自资源的默认图像设置了我的图片框:
public Form1()
{
InitializeComponent();
pictureBox1.Image = Properties.Resources.default_Employee_Image;
}
when I check if the picture in the pictureBox is from resources with this code, it doesn't draw the border:当我使用此代码检查图片框中的图片是否来自资源时,它不会绘制边框:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
//pictureBox border color
Color themeColor = Color.FromArgb(172, 188, 212);
if (pictureBox1.Image == null) //if image from db is null display default image
{
pictureBox1.Image = Properties.Resources.default_Employee_Image;
}
//if image is the default image: paint border
if (pictureBox1.Image == Properties.Resources.default_Employee_Image)
{
ControlPaint.DrawBorder(e.Graphics, pictureBox1.ClientRectangle, themeColor, ButtonBorderStyle.Solid);
}
}
Also if image from the pictureBox is default, I'll save it as null in my database.此外,如果图片框中的图像是默认的,我会将其保存为 null 在我的数据库中。
I want to only have a border if the default image is loaded.如果加载了默认图像,我只想有一个边框。 If I select another image the border should be gone.
如果我 select 另一个图像边框应该消失。
If the pictureBox has the default image it should look like this (which means that I still have not selected a picture of the user):如果图片框有默认图像,它应该是这样的(这意味着我还没有选择用户的图片):
But if the picture in my pictureBox is not the default image-(the image in my resources), [This means that I already selected an image] it should look like this:但是如果我的pictureBox中的图片不是默认图片-(我的资源中的图片),[这意味着我已经选择了一张图片]它应该是这样的:
And not like this:而不是这样:
pictureBox1.Image == Properties.Resources.SomeImage
will never return true
, because every time that you call Properties.Resources.SomeImage
, it returns a new instance of the image. pictureBox1.Image == Properties.Resources.SomeImage
永远不会返回true
,因为每次调用Properties.Resources.SomeImage
时,它都会返回图像的新实例。
You don't need to track the assigned image;您不需要跟踪分配的图像; instead, you need to track the status .
相反,您需要跟踪状态。 You can use either of the following options:
您可以使用以下任一选项:
Rely on a flag: You can set a flag like bool isDefaultImage = true;
依赖一个标志:你可以设置一个标志,比如
bool isDefaultImage = true;
at form level and if at some point of your application you changed the image, set it to true.在表单级别,如果您在应用程序的某个时刻更改了图像,请将其设置为 true。 Something like this:
像这样的东西:
if(isDefaultImage) { //draw border }
Rely on Model/DataSource: You can also rely on your model/data source values and instead of checking UI elements, check if the user has a profile picture.依赖模型/数据源:您还可以依赖模型/数据源值,而不是检查 UI 元素,而是检查用户是否有个人资料图片。 Something like this:
像这样的东西:
if(myUserObject.ProfilePicture == null) { //draw border }
Compare two images比较两张图片
Anyhow, just in case you are interested to compare two Image
objects to see whether they are same Image, you can use the following method:无论如何,如果您有兴趣比较两个
Image
对象以查看它们是否是相同的 Image,您可以使用以下方法:
public bool AreImagesEqual(Image img1, Image img2)
{
ImageConverter converter = new ImageConverter();
byte[] bytes1 = (byte[])converter.ConvertTo(img1, typeof(byte[]));
byte[] bytes2 = (byte[])converter.ConvertTo(img2, typeof(byte[]));
return Enumerable.SequenceEqual(bytes1, bytes2);
}
Then you can use it like this:然后你可以像这样使用它:
using(var image = Properties.Resources.DefaultImage)
{
var isDefaultImage = AreImagesEqual(pictureBox1.Image, image);
if(isDefaultImage)
{
//draw border
}
}
I have found a solution for my problem, I have changed my approach (Rely on a flag approach [by @Reza Aghaei]), and instead of comparing pictureBox image with the resource image, I just checked if pictureBox1.Tag is null or not:我找到了解决问题的方法,我改变了方法(依靠标志方法 [by @Reza Aghaei]),而不是将图片框图像与资源图像进行比较,我只是检查了图片框1.标签是否为 null :
On my constructor:在我的构造函数上:
public Form1()
{
InitializeComponent();
pictureBox1.Image = Properties.Resources.default_Employee_Image; //the default image from resources
pictureBox1.Image = null;
}
On my dataGridView cell click event:在我的 dataGridView 单元格单击事件中:
private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.RowIndex != -1)
{
//Display user image
using (SqlConnection con = new SqlConnection(connectionStringConfig))
using (SqlCommand sqlCmd = new SqlCommand("SELECT user_image FROM dbo.Employee_Image WHERE employee_id=@employee_id", con))
{
con.Open();
sqlCmd.Parameters.Add("@employee_id", SqlDbType.NVarChar).Value = EmployeeId;
using (SqlDataReader reader = sqlCmd.ExecuteReader())
{
if (reader.HasRows)
{
reader.Read();
pictureBox1.Image = ImageOperations.BytesToImage((byte[])(reader.GetValue(0)));
if (reader.GetValue(0) == null) //if image is null add border color to tag
{
pictureBox1.Tag = my_color_here;
}
else
{
pictureBox1.Tag = null;
}
}
else
{
pictureBox1.Image = null;
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Something is wrong with the selected record! \nError: { ex.GetType().FullName }");
}
}
On BytesToImage method in ImageOperations class: ImageOperations class 中的 BytesToImage 方法:
public static Image BytesToImage(byte[] buffer) //Get image from database
{
using (MemoryStream ms = new MemoryStream(buffer))
{
return Image.FromStream(ms);
}
}
And on my pictureBox paint event:在我的图片框绘画事件中:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
//if image is not present in the picturebox -> paint its border
if (pictureBox1.Image == null)
{
pictureBox1.Tag = my_color_here;
pictureBox1.Image = Properties.Resources.default_Employee_Image;
}
if (pictureBox1.Tag != null) //if tag has a value -> paint the border (this happens if image form db is null)
{
ControlPaint.DrawBorder(e.Graphics, pictureBox1.ClientRectangle, my_color_here, ButtonBorderStyle.Solid);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.