How can i create a thumbnail from a video binary (bytes[])?
I do not have access to the directories, so i can't save the file and the example that I can see (using ffmpeg) to create a thumbnail is from a path.
Is there another way to do it?
Assuming, sampleBytes is our byte[] :
using (MemoryStream ms = new MemoryStream(sampleBytes))
{
Bitmap thumb = new Bitmap(100, 100);
using (Image bmp = Image.FromStream(ms))
{
using (Graphics g = Graphics.FromImage(thumb))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.DrawImage(bmp, 0, 0, 100, 100);
}
}
// this can show the result
picBoxOut.Image = thumb;
}
Related
I'm trying to clean the PictureBox in my Windows Form App before placing an image after it's been interpolated. I've searched for similar situations, but stuff like:
pictureBox1.Image=null;
pictureBox1.InitialImage=null;
pictureBox1.Invalidate();
pictureBox1.Dispose();
don't work in any combination. I used this method to open the image and put it on the PictureBox:
void openImage()
{
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
file = System.Drawing.Image.FromFile(openFileDialog1.FileName);
pictureBox1.Image = file;
opened = true;
}
}
After that the image is interpolated with HighQualityBicubic, but the outcome is being placed on top of the original image. I used the Zoom property of PictureBox, so it won't crop the image or cover up the rest of the form.
Is there any way to remove the original picture and enlarge the outcome in the box?
Interpolation method (the guilty part of the code):
void bicubic(int newHeight,int newWidth)
{
Bitmap newImage = new Bitmap(file);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(newImage, 0, 0, newWidth, newHeight);
}
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
pictureBox1.Refresh();
//pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = newImage;
}
You have to create a image with out come size. In your code you are creating the new image with original image size
Bitmap OrgImage = new Bitmap(file);
Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(OrgImage, 0, 0, newWidth, newHeight);
}
pictureBox1.Image = newImage;
this may solve your problem
PictureBox.SizeMode = PictureBoxSizeMode.StretchImage will stretch the image for you.
If you want other Sizing properties, just scroll trough the PictureBoxSizeMode enum.
As for reassignment, setting the PictureBox's Image property to null should be enough
I am trying to print 40x40mm labels from a programmatically created image.
The label must have text on it, and a logo. Since the label is fairly small I am finding myself fiddling with how to do proper smooting, antialias and such.
I have tried multipl settings but I am not sure it's even the right way to go about it.
First I draw the container Bitmap:
private Bitmap DrawLabelCircle()
{
var labelImage = new Bitmap(152, 152);
using (Graphics gfx = Graphics.FromImage(labelImage))
{
var pen = new Pen(Color.Black, 1);
gfx.SmoothingMode = SmoothingMode.AntiAlias;
gfx.DrawEllipse(pen, 1, 1, 150, 150);
}
return labelImage;
}
Then I overlay different text snippets on that container Bitmap
private Bitmap DrawDistributorTextRectangle(string text)
{
var bitmap = new Bitmap(113, 113);
var rectangle = new Rectangle(0, 0, 110, 110);
using (Graphics gfx = Graphics.FromImage(bitmap))
{
gfx.SmoothingMode = SmoothingMode.AntiAlias;
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
var font = new Font(FontFamily.GenericSansSerif, 5, FontStyle.Regular, GraphicsUnit.Point);
var brush = new SolidBrush(Color.Black);
gfx.TextRenderingHint = TextRenderingHint.AntiAlias;
gfx.DrawString(text, font, brush, rectangle);
}
bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
return bitmap;
}
Overlay that text on the previous created Bitmap.
private Bitmap DistributorTextOverlay(Bitmap source, Bitmap overlay)
{
var result = new Bitmap(source.Width, source.Height);
var graphics = Graphics.FromImage(result);
graphics.CompositingMode = CompositingMode.SourceOver;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(source, 0, 0);
graphics.DrawImage(overlay, 120, 0);
return result;
}
And the I save it.
var imageCodecInfo = ImageCodecInfo.GetImageEncoders().First(encoder => encoder.MimeType == "image/png");
var encoderInfo = new EncoderParameters() { Param = { [0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L) } };
image.SetResolution(203, 203);
image.Save("img.png", imageCodecInfo, encoderInfo);
The big challenge here is that the image I get is actually looking alright, all things considered.
But when I print it, it looks terrible pixelated.
I would really like to give some pointers for what settings I should apply to all these bitmaps before saving the final result, and what settings should apply for the final image I save.
I am by no means a .NET graphics expert so all help is much appreciated.
40mm is 1.5748 inches. So if you plan to print it at 300 dpi resolution, your bitmap should be 1.5748*300 = 472 pixels instead of 152.
I have a PNG image of resolution 6000x4000, on which I have to draw upon. So I load the image into a pictureBox with of size 1280x800. After drawing on it, I need to save the PNG image in its original resolution of 6000x4000. So I reload it into a new bitmap of size 6000x4000 using
btm = new Bitmap(6000, 4000);
image = Graphics.FromImage(btm);
g.DrawImage(btm, Point.Empty);
And save it using
btm.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
Now I end up with the a white background png image of resolution 6000x4000 but with the edited image of 1280x800 on it like this Saved Image
How do I resize the image back to its original (6000x4000) size. Thank you.
Also please find my codebelow
private void drawImage(string imgLocation)
{
Bitmap b = new Bitmap(imgLocation);
////test
pictureBox1.Height = 800;
pictureBox1.Width = 1280;
g = pictureBox1.CreateGraphics();
btm = new Bitmap(6000, 4000);
image = Graphics.FromImage(btm);
image.CompositingMode = CompositingMode.SourceCopy;
image.CompositingQuality = CompositingQuality.HighQuality;
image.InterpolationMode = InterpolationMode.HighQualityBicubic;
image.SmoothingMode = SmoothingMode.HighQuality;
image.PixelOffsetMode = PixelOffsetMode.HighQuality;
image.Clear(Color.White);
image.DrawImage(b, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
//image.DrawImage(btm, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
//g.DrawImage(btm, Point.Empty);
g.DrawImage(btm, new Rectangle(0, 0, 6000,4000) );
}
What about this? g.DrawImage(btm, new Rectangle(0,0,6000,4000));
Below is a method that may help you with images upscaling/downscaling.
Note though that big upscaling ratio will cause serious image distortions in any case.
You may also try to play with other then graphics.CompositingMode properties like graphics.CompositingQuality
You will need following usings:
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
The method works fine in my app.
public static Image ImageResize(Image img, int width, int height)
{
var tgtRect = new Rectangle(0, 0, width, height);
var tgtImg = new Bitmap(width, height);
tgtImg.SetResolution(img.HorizontalResolution, img.VerticalResolution);
using (var graphics = Graphics.FromImage(tgtImg))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(img, tgtRect, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, wrapMode);
}
return tgtImg;
}
}
I'm trying to resize an image. I thought it was a simple task...
Here's my code (note, the two Save calls are just for debugging to illustrate the problem):
var newSize = new Size { Width = 450, Height = 250 };
using (var img = (Bitmap)Image.FromFile(sourceImageFilename))
{
var outputImage = new Bitmap(newSize.Width, newSize.Height);
// Save input image for debugging (screenshot below)
img.Save(#"M:\Coding\Photos\Temp\input.jpg");
using (Graphics gr = Graphics.FromImage(img))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(outputImage, new Rectangle(0, 0, newSize.Width, newSize.Height));
}
// Save output image for debugging (screenshot below)
outputImage.Save(#"M:\Coding\Photos\Temp\output.jpg");
}
This appears to be the exact same code a ton of people are using (and exists on SO in many answers). However, here's what the two images that are being written to disk look like:
The original image is 5344x3006 and newSize (and the black output image) are 450x250.
All my other code is working fine (reading pixels from the input image with SetPixel, etc.), it's just this resize that's broken. Doing the resize with the Bitmap constructor is fine (but a bad quality resize).
You need to get the graphics from the OutputImage.
public static Bitmap Scale(this Bitmap inputImage, Size newSize)
{
var outputImage = new Bitmap(newSize.Width, newSize.Height);
inputImage.Save(#"M:\Coding\Photos\Temp\input.jpg");
using (Graphics gr = Graphics.FromImage(outputImage))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(inputImage, new Rectangle(0, 0, newSize.Width, newSize.Height));
}
outputImage.Save(#"M:\Coding\Photos\Temp\output.jpg");
return outputImage;
}
I am having image sharing application where users upload images and I take thumbnails of these images...how ever , everything is working fine but sometimes the image thumbnail(600 * 800) size is almost 1 mb which is very huge is there anyway to modify the image resolution or something to make the size like..100 kb or something . this is my code .
Bitmap bmp = new Bitmap(Width, Height);
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, Width, Height);
System.Drawing.Size rs = new System.Drawing.Size();
rs.Height = Height;
rs.Width = Width;
gr.DrawImage(originalImage, new Rectangle(new Point(0, 0), rs), 0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel);
string thumbnailPath = string.Concat(pathToSaveIn, thumbnailName);
bmp.Save(thumbnailPath);
gr.Dispose();
The image resizing code looks OK (at first glance). However, you're saving the image in bitmap format, which is lossless -- hence the large size of the file.
You probably want to use JPEG instead for a thumbnail: for photographs, etc., this gives good compression.
This may help:
public void SaveImage(Bitmap image, string filename)
{
long quality = 80L; // adjust as appropriate
var qualityEncoder = Encoder.Quality;
using (var encoderParameter = new EncoderParameter(qualityEncoder, quality))
using (var encoderParams = new EncoderParameters(1))
{
encoderParams.Param[0] = encoderParameter;
var jpegEncoder = GetEncoder(ImageFormat.Jpeg);
image.Save(filename, jpegEncoder, encoderParams);
}
}
private static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
return codecs
.Where(codec => codec.FormatID == format.Guid)
.FirstOrDefault();
}
This looks like .NET. Scott Hanselman had a good blog post on this. Essentially a review of a package on NuGet that helps with this.
http://www.hanselman.com/blog/NuGetPackageOfWeek11ImageResizerEnablesCleanClearImageResizingInASPNET.aspx
Try System.Drawing.Image.GetThumbnailImage. I haven't used it myself, but looks like it might work.