Adding transparent Gif images to list view in c# - c#

I am trying to add a transparent Gif image to list view, I used the following code snippet
to add gif image.
// control as ListView
void AddGiftoListView(Control parent)
{
Bitmap img = (Bitmap)Properties.Resources.madLoader; // Transparent gif image
Size sz = img.Size;
PictureBox pbGif = new PictureBox();
pbGif.Size = img.Size;
pbGif.Image = img;
parent.Controls.Add(pbGif);
Point p = new Point(1, 1);
pbGif.Location = p;
pbGif.Show();
}
But the gif is not transparent, when the list view filled with text, we can see gif on top of that with white background. is there any way ro resolve this issue?
Thanks,
Shiju P K

Try making your form's TransparencyKey as white
this.TransparencyKey = Color.White;

Related

Label Image mode to stretch

I wrote this code to add my Labels:
JArray a = JArray.Parse(temp);
Label[] labels = new Label[100];
foreach (JObject o in a.Children<JObject>())
{
foreach (JProperty p in o.Properties())
{
string name = p.Name;
string value = p.Value.ToString();
if (name == "name")
{
labels[counter] = new Label();
//Image i = Image.FromFile("item.jpg");
labels[counter].Text = value;
labels[counter].Image =Image.FromFile("item.jpg");
//labels[counter].Image
//labels[counter].BackColor = Color.Blue;
labels[counter].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
labels[counter].Top = height;
height += 50;
Controls.Add(labels[counter]);
}
}
}
The Image should stretch to the Label Size. How can I do this?
The abilities to show and manipulate images and text are spread out in a rather wild fashion among Winforms controls.
A Label can not stretch its Image.
A PictureBox and a Panel can but they don't show their Text
A Button can do both but will always be a Button, no matter how you style it..
So to get a combination you will need to either owner-draw something:
DrawImage in an overload to get the right size of the image, then add Image to Label
Or DrawString the Text onto a Panel to show it alongside the Image
or you could combine two controls with the right abilities:
You can create a Panel and set its BackgroundImage to your Image and its BackgroundImageLayout=Stretch. Then you can add your Label with its Text set to the Panel's controls collection:
// preparation for testing:
Image image = Image.FromFile("D:\\stop32.png");
Size size = new Size(77, 77);
// create the combined control
// I assume your Label is already there
Panel pan = new Panel();
pan.Size = size;
// or, since the Label has the right size:
pan.Size = label.Size; // use Clientsize, if borders are involved!
pan.BackgroundImage = image;
pan.BackgroundImageLayout = ImageLayout.Stretch;
label.Parent = pan; // add the Label to the Panel
label.Location = Point.Empty;
label.Text = "TEXT";
label.BackColor = Color.Transparent;
// add to (e.g.) the form
pan.Parent = this;
Set Borders as you like..
One more option: If all Images should have the same Size and if it is 256x256 pixels or less you could add them to an ImageList. This will stretch them to the ImageList.ImageSize in a very simple way and you can add them to your Label..
Very simple:
VB
Label1.Image = New Bitmap(Image.FromFile("Screenshot.jpg"), Label1.Size)
C#
Label1.Image = new Bitmap(Image.FromFile("Screenshot.jpg"), Label1.Size);
If you are using WinForms you try try below:
labels[counter].Size =
new Size(labels[counter].Image.Width, labels[counter].Image.Height);
This works perfect for me:
Just set the image in design mode (don't use imagelist) use the "Image" option
if we have label1 as the label where we want the image, put the next line inside the constructor:
label1.Image = new Bitmap(label1.Image, label1.Size);
I was trying the solution of Zibri, but deform the image in my case.

How do I find a certain Child image in a Canvas?

I have a Canvas that has 400 children. Each of the children is a rectangle and is filled with an Image. If I want to find a certain image, how would I go doing about that?
//My code to fill a rectangle
Image img = new Image();
img.Source = new BitmapImage(new Uri(#"hero.png", UriKind.Relative));
img.Margin = rec.Margin;
ImageBrush imgbrush = new ImageBrush();
imgbrush.ImageSource = img.Source;
rec.Fill = imgbrush;
//My attempt at finding that certain rectangle
foreach (Rectangle rec in canvas1.Children)
{
if (rec.Fill = ImageBrush.ImageSourceProperty) // I tried to compare the rectangle with the image's source
{
}
}
You may use names for for every specific Image then using FindName Method
object wantedNode = stackPanel.FindName("dog");
Why not store the objects in an array (key/pair) that you can later iterate through?

Printing bitmap from Silverlight - image blurry

I'm trying to print image from Silverlight application. I have pretty good quality scans (TIFF) with resolution 1696x2200
When I print - I get PrintableArea from PrintDocument and it's 816x1056
What I do - I resize bitmap to Printable area (to fit document to page) and result I get is blurry image. I understand this is scaling problem (most likely), but how do I scale properly so it looks good? When I display document inside Image and just set image size - it looks good.
For resizing I'm using WriteableBitmapEx extensions and tried both types of resize (Nearest neighbor and bilinear)
Code:
var printDocument = new PrintDocument();
printDocument.PrintPage += (s, ea) =>
{
var printableArea = ea.PrintableArea;
var bitmap = this.currentPreviewPage.FullBitmap.Resize((int)printableArea.Width, (int)printableArea.Height, WriteableBitmapExtensions.Interpolation.Bilinear);
var image = new Image { Source = bitmap };
var canvas = new Canvas { Width = bitmap.PixelWidth, Height = bitmap.PixelHeight };
canvas.Children.Add(image);
ea.PageVisual = canvas;
ea.HasMorePages = false;
};
printDocument.PrintBitmap("Silverlight Bitmap Print");
How document looks on screen (inside Image)
And this is printed:
Rather than using the WriteableBitmapEx extensions, when declaring your Image element, try setting the Stretch property so that it stretches based on your maximum specified dimensions:
var image = new Image { Source = bitmap, Stretch = Stretch.UniformToFill };
Blilinear filter tends to blur images.You may want to try WriteableBitmapExtensions.Interpolation.NearestNeighbor instead to see if you get better results
In my case it was enough to set UseLayoutRounding="True".

Icon to Image - transparency issue

I'm trying to build a treeview like file list in a richtext box.
It should look like an explorer treeview. My code is able to get an resize the icon, but the transparency is missing (light gray background instead of transparency). What do I need to change here? Is the Image format wrong?
Is there a better way to add an image to a richtextbox?
// Get file info
FileInfo f = new FileInfo("myfile.name");
// Get icon for fileinfo
Icon ico = Icon.ExtractAssociatedIcon(f);
// Convert icon to bitmap
Bitmap bm = ico.ToBitmap();
// create new image with desired size
Bitmap img = new Bitmap(16,16,PixelFormat.Frmat32bpRgb);
// Create graphics with desired sized image
Graphics g = Graphics.FormImage(img);
// set interpolation mode
g.InterpolationMode = InterpolationMode.HighQualityBiCubic;
// draw/resize image
g.DrawImage(bm, new Rectangle(0,0,16,16), new Rectangle(0, 0, bm.Width, bm,Height), GraphicalUnit.Pixel);
// Paste to clipboard
Clipboard.SetImage(bm);
// Paste in RichtextBox
rtb.Paste();
Example:
Edit:
I've figured out that the image is transparent, but using Clipboard.SetImage() doesn't publish it as transparent image.
Any ideas why and what can I do to fix it? Do I need to switch to a differn textbox control?
I've had some luck going through Graphics.
Bitmap b = new Bitmap(pbAssetLoaded.Width, pbAssetLoaded.Height);
using (Graphics g = Graphics.FromImage(b))
{
g.DrawIcon(SystemIcons.Information, 0, 0);
}
This draws the icon with transparency to the Bitmap.
Try
img.MakeTransparent();
after you contruct it.
Note that this will change your PixelFormat to Format32bppArgb.

C# Forms Picturebox to show a solid color instead of an image

Im making a little app to display the pictures of guests as they scan their cards.
But i want to to display blank green or red (green if the guest exists without a photo and red if they dont exist)
But i cant figure out how to create a blank colour image.
Bitmap bmRed = new Bitmap(imgbox.Width, imgbox.Height, PixelFormat.Format24bppRgb);
imgbox.Image = bmRed;
Thats the code i have at the moment and it just makes the box black.
imgbox is a PictureBox
Don't use an image - set the BackColor property of the PictureBox:
imgBox.BackColor = Color.Red;
To prevent null pointer exception, create a blank bmp
myPicBox.Image = new Bitmap(myPicBox.Width, myPicBox.Height);
Graphics graphics = Graphics.FromImage(myPicBox.Image);
Brush brush = new SolidBrush(Color.Gray);
graphics.FillRectangle(brush, new System.Drawing.Rectangle(0, 0, myPicBox.Width, myPicBox.Height));
How about setting the background color directly?
imgbox.BackColor = Color.Red;
create a graphics context and draw using it.
using(Graphics g = Graphics.FromImage(bmRed))
{
g.FillRectangle(new SolidBrush(Color.Red),0,0,imgbox.Width,imgbox.Height);
}
Single statement:
Graphics.FromImage(PicBox.Image=new bitmap(PicBox.Size)).FillRectangle(Brushes.Red,new Rectangle (Point.EMPTY,PicBox.Size));

Categories

Resources