6 Replies - 1610 Views - Last Post: 30 March 2010 - 10:06 AM Rate Topic: -----

#1 mulhearn22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-March 10

Image Transparency Leaves Border

Posted 30 March 2010 - 06:39 AM

Hi,

Please follow this thread so you're caught up. My issue is at the bottom. Any and all help would be really appreciated. It's driving me nuts!

And if I change colors, the border changes, so it's definitely something to do with the .Clear() and .MakeTransparent().

TIA,
Matt

Is This A Good Question/Topic? 0
  • +

Replies To: Image Transparency Leaves Border

#2 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Image Transparency Leaves Border

Posted 30 March 2010 - 07:31 AM

I may be wrong, but I think the problem is with your image. Are you using an alpha channel? Open the image in Photoshop, or Gimp (or whatever) and make sure that you are using an alpha channel. Save the image as a png and use it with the picture box. Don't worry about clearing the graphics or anything like that.

Could attach the image?
Was This Post Helpful? 0
  • +
  • -

#3 mulhearn22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-March 10

Re: Image Transparency Leaves Border

Posted 30 March 2010 - 07:59 AM

The purpose of this is to allow users to upload avatars. I want to resize the avatar to 100x100 and to not distort it. In the example I've attached, it keeps ratio so the left and right of the image should be blank. Originally, it filled the empty space with black, but the Clear() and MakeTransparent() took care of that but leaves the border on the left and top. If I can get rid of that border, everything would be perfect.

Here is my code as well:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace Presents
{
public static class ImageResizer
{
#region ResizeImage(Bitmap, int, int)
public static Image ResizeImage(Image img, int width, int height)
{
int sourceWidth = img.Width;
int sourceHeight = img.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;

float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;

nPercentW = ((float)width / (float)sourceWidth);
nPercentH = ((float)height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((width -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((height -
(sourceHeight * nPercent)) / 2);
}

int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap bmPhoto = new Bitmap(width, height,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(img.HorizontalResolution,
img.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Green);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;

grPhoto.DrawImage(img,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);

grPhoto.Dispose();

bmPhoto.MakeTransparent(Color.Green);

return bmPhoto;
} //ResizeImage(Image, int, int)
#endregion
}
}

Attached image(s)

  • Attached Image

This post has been edited by mulhearn22: 30 March 2010 - 08:01 AM

Was This Post Helpful? 0
  • +
  • -

#4 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Image Transparency Leaves Border

Posted 30 March 2010 - 08:30 AM

Why not just create a PictureBox sized to 100 x 100, and set the size mode to "Zoom"? The PictureBox will automatically resize the image (while keeping the ratio), and will handle the image transparency.
Was This Post Helpful? 0
  • +
  • -

#5 mulhearn22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-March 10

Re: Image Transparency Leaves Border

Posted 30 March 2010 - 09:12 AM

I'm sorry, I should have specified that this is a web application so I need to fix it. We don't have a picture box control.
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Image Transparency Leaves Border

Posted 30 March 2010 - 09:26 AM

Moving to ASP.NET then :)
Was This Post Helpful? 0
  • +
  • -

#7 mulhearn22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-March 10

Re: Image Transparency Leaves Border

Posted 30 March 2010 - 10:06 AM

Well, I mean, it really doesn't matter if it's ASP.NET or not. Sure I can't use that control, but it's not about the control. It's about getting C# code to work, so being in the C# forum makes sense... I need this freaking line to disappear!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1