How to generate image as Thumb in simple way i think minimal coding on asp.net,
Then after how to save particular folder
I would not recommend to use the "Image.GetThumbnailImage" method because of the poor quality
(search "Image.GetThumbnailImage quality" on your favorite search engine...)
Here is a code snippet:
void GenerateThumbnail(string sourceImagePath, string destImagePath, int width, int height, System.Drawing.Imaging.ImageFormat destImageFormat)
{
using (var destImage = new System.Drawing.Bitmap(width, height))
{
using (var g = System.Drawing.Graphics.FromImage(destImage))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
using (var sourceImage = System.Drawing.Image.FromFile(sourceImagePath))
{
g.DrawImage(sourceImage, new System.Drawing.Rectangle(0, 0, width, height));
}
destImage.Save(destImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
OR - you can use Piczard (Piczard)
Here are some usage examples:
// 50% thumbnail (Jpeg image format)
var filter = new CodeCarvings.Piczard.ImageTransformation(50);
filter.SaveProcessedImageToFileSystem("~/MySourceImage.jpg", "~/MyDestImage.jpg");
// Fixed size thumbnail 400x200 (PNG image format)
var filter2 = new CodeCarvings.Piczard.FixedResizeConstraint(400, 200);
filter.SaveProcessedImageToFileSystem("~/MySourceImage2.jpg", "~/MyDestImage2.png");
I have tried like this so cool..!!,
public void ThumbGenerate(string sourcepath,string thumbsavepath, int width, int height)
{
Image image = Image.FromFile(sourcepath);
Image thumb = image.GetThumbnailImage(width, height, () => false, IntPtr.Zero);
thumb.Save(Path.ChangeExtension(thumbsavepath, "jpg"));
}
Note :You can change extension what you like something jpg,jpeg,png
Related
My target is to generate image with A5 format and place some text on it. At the beggining i was trying to accomplish that using System.Drawing.Imaging but seems like Xamarin is not able to work with this. Then I found out SkiaSharp library which seems like would be valid to do this job but i am a bit lost here. How can i correctly generate an image, put some text on it and send via Stream. Note that if there is more text that couldn't fit on single A5 next image should be created.
This is what i got so far:
private void CreateBitmapFromText(List<string> texts)
{
using var surface = SKSurface.Create(width: 640, height: 480, SKColorType.Gray8, SKAlphaType.Premul);
SKCanvas myCanvas = surface.Canvas;
// clear the canvas / fill with white
myCanvas.DrawColor(SKColors.White);
// set up drawing tools
using (var paint = new SKPaint())
{
paint.TextSize = 64.0f;
paint.IsAntialias = true;
paint.Color = new SKColor(0x42, 0x81, 0xA4);
paint.IsStroke = false;
// draw the text
foreach (var text in texts)
{
myCanvas.DrawText(text, ??, ??, paint);
}
}
}
EDIT: with help of #Jason i did this:
var aaa = ToStream(CreateBitmapFromText(new List<string>() {"asas", "vvvv"}), SKEncodedImageFormat.Png);
_printService.PrintImage(aaa);
public Stream ToStream(SKImage image, SKEncodedImageFormat format)
{
SKData encoded = image.Encode(format, 90);
return encoded.AsStream();
}
public SKImage CreateBitmapFromText(List<string> texts)
{
using var surface = SKSurface.Create(width: 640, height: 480, SKColorType.Gray8, SKAlphaType.Premul);
SKCanvas myCanvas = surface.Canvas;
// clear the canvas / fill with white
myCanvas.DrawColor(SKColors.White);
// set up drawing tools
using (var paint = new SKPaint())
{
paint.TextSize = 64.0f;
paint.IsAntialias = true;
paint.Color = new SKColor(0x42, 0x81, 0xA4);
paint.IsStroke = false;
// draw the text
foreach (var text in texts)
{
myCanvas.DrawText(text, 0.0f, 0.0f, paint);
}
}
return surface.Snapshot();
}
When i send it to printer i see this (white paper with some little black chars ?? at the top?)
I was able to easily modify the Xamarin FramedText sample to save a bitmap by adding this to the end of OnCanvasViewPaintSurface
// get the SKImage from SKSurface
var image = surface.Snapshot();
// Encode as PNG, returns SKData
var data = image.Encode(SKEncodedImageFormat.Png, 100);
string path = Path.GetTempFileName();
// write byte[] to file, verify data with image viewer
File.WriteAllBytes(path, data.ToArray());
you should be able to easily modify this to return a byte[] or stream
I am trying to resize a SystemIcon for use within a ErrorProvider.
Dim WarnProvider As New ErrorProvider
WarnProvider.BlinkStyle = ErrorBlinkStyle.NeverBlink
WarnProvider.Icon = SystemIcons.Information.Clone()
WarnProvider.Icon.Size = New Size(16,16)
But the SystemIcons has the size set as a read only property.
Been messing with it for the past hour and have not found any good methods to make this work.
Can someone help?
Thanks
I was looking for the a method to do this as well, and came across this post. Here's what I ended up doing to resolve the issue.
I created a global static method to resize an icon.
public static class Global
{
public static Icon ResizeIcon( Icon icon, Size size )
{
Bitmap bitmap = new Bitmap(size.Width,size.Height);
using( Graphics g = Graphics.FromImage(bitmap) )
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(icon.ToBitmap(), new Rectangle(Point.Empty,size));
}
return Icon.FromHandle(bitmap.GetHicon());
}
}
Then I applied the icon in the constructor of the form after InitializeComponent() was called.
public SpecificationsDialog( int pid )
{
InitializeComponent();
warningProvider1.Icon = Global.ResizeIcon(SystemIcons.Warning,SystemInformation.SmallIconSize);
}
I was looking for the same thing and found the answer elsewhere, so I'll post here
http://www.codeproject.com/Questions/242780/error-provider-problem
WarnProvider.Icon = new Icon (SystemIcons.Warning, 16, 16);
or
WarnProvider.Icon = new Icon (WarnProvider.Icon, 16, 16);
I changed Drew's solution a little to be an extension method for the errorprovider:
public static ErrorProvider SetIcon(this ErrorProvider errorProvider, Icon icon, Size size)
{
Bitmap bitmap = new Bitmap(size.Width, size.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(icon.ToBitmap(), new Rectangle(Point.Empty, size));
}
errorProvider.Icon = Icon.FromHandle(bitmap.GetHicon());
return errorProvider;
}
Then it's can be used like so:
ErrorProvider ep = new ErrorProvider();
ep.SetIcon(SystemIcons.Asterisk, new Size(16,16));
I need to do two things to an image: resize it, then crop it.
I'm resizing like this:
nonResizedImage = new Bitmap(imagePath);
Bitmap scaled = new Bitmap(preCropWidth, preCropHeight);
using (Graphics scaledGraphics = Graphics.FromImage(scaled))
{ // scale image to the sizeo f the image the user cropped on
scaledGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
scaledGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
scaledGraphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
scaledGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
scaledGraphics.Clear(ColorTranslator.FromHtml("#FFFFFF"));
scaledGraphics.DrawImage(nonResizedImage, 0, 0, preCropWidth, preCropHeight);
}
Now I need to crop the image. I've found a function that does this:
static byte[] Crop(string Img, int Width, int Height, int X, int Y)
{
try
{
using (SD.Image OriginalImage = SD.Image.FromFile(Img))
{
using (SD.Bitmap bmp = new SD.Bitmap(Width, Height))
{
bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
{
Graphic.SmoothingMode = SmoothingMode.AntiAlias;
Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
Graphic.DrawImage(OriginalImage, new SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, OriginalImage.RawFormat);
return ms.GetBuffer();
}
}
}
}
catch (Exception Ex)
{
throw Ex;
}
}
But this requires a image as input. So, I could save the output of my resize code to the disk, then read it back in again to do the crop, but this seems needlessly inefficient. I don't really know much about image manipulation in c# though.
How do I crop the scaledGraphics I have, without first saving it to the disk?
One of the overloads for new Bitmap is Width, Height, Graphics Object. You should be able to just pass the graphics object in and then create the bitmap from that. Something like this
static byte[] Crop(Graphics g, int Width, int Height, int X, int Y)
{
try
{
using (Bitmap bmp = new Bitmap(Width, Height, g))
{
...
}
}
......
}
I wrote these functions which I use in conjunction to scale down some BitmapData and save it as a PNG. However, the scaledImage.draw(originalImage, scalingMatrix, null, null, null, true); line with smoothing set to true does not have the intended smoothing effect when I save the bitmap data as a PNG using the second function. The resulting image file is not antialiased at all. Is there anything I'm doing wrong here? Thanks!
public static function scaleImage(originalImage:BitmapData, size:int):BitmapData
{
// Calculate the scaled size.
var scale:Number;
var scaledWidth:Number;
var scaledHeight:Number;
if (originalImage.width > originalImage.height)
{
scale = (size as Number) / (originalImage.width as Number);
scaledWidth = size;
scaledHeight = originalImage.height * scale;
}
else
{
scale = (size as Number) / (originalImage.height as Number);
scaledHeight = size;
scaledWidth = originalImage.width * scale;
}
var scalingMatrix:Matrix = new Matrix();
scalingMatrix.scale(scale, scale);
// Scale the image.
var scaledImage:BitmapData = new BitmapData(scaledWidth, scaledHeight, true, 0x00000000);
scaledImage.draw(originalImage, scalingMatrix, null, null, null, true);
return scaledImage;
}
public static function saveImageAsPNG(image:BitmapData, imageFile:File):void
{
// Encode the image as a PNG.
var pngEncoder:PNGEncoder = new PNGEncoder();
var imageByteArray:ByteArray = pngEncoder.encode(image);
// Write the image data to a file.
var imageFileStream:FileStream = new FileStream();
imageFileStream.open(imageFile, FileMode.WRITE);
imageFileStream.writeBytes(imageByteArray);
imageFileStream.close();
}
Turns out this code was working. It was saving smoothed images. It wasn't apparent because the images I was scaling down were ~20K x 20K pixels so jaggies appeared anyway with smoothing. Smoothing was apparent with more "normal" sized images like 2K x 2K pixels
i use this code to create thumbnails
System.Drawing.Image.GetThumbnailImageAbort abort = new System.Drawing.Image.GetThumbnailImageAbort(this.ThumbnailCallback);
System.Drawing.Image image2 = image.GetThumbnailImage((int)Math.Round((double)wid / difference), (int)Math.Round((double)hei / difference), abort, IntPtr.Zero);
image2.Save(str2, System.Drawing.Imaging.ImageFormat.Jpeg);
image2.Dispose();
but i get this very low quality image
but it is suposed to be like this one
what i am making wrong
or how can achieve this
Your problem is not really with the GetThumbnailImage() method, but instead in how you are saving the file. You need to specify the quality level of the JPEG you are saving, or it seems it always defaults to a very low value.
Consider this code as a guide (it's from an old .NET 2.0 project; the code still works fine compiled against 4.0, but there may be a more direct method in 4.0; I've never had reason to check)
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegEncoder = null;
for (int x = 0; x < encoders.Length; x++) {
if (string.Compare(encoders[x].MimeType, "image/jpeg", true) == 0) {
jpegEncoder = encoders[x];
break;
}
}
if (jpegEncoder == null) throw new ApplicationException("Could not find JPEG encoder!");
EncoderParameters prms = new EncoderParameters(1);
prms.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80L);
bitmap.Save(fileName, jpegEncoder, prms);
Here is another solution that should always work without fetching out the encoder. It resizes keeping relation between width & heigh ... modify for your needs.
/// <summary>
/// Resize an image with high quality
/// </summary>
public static Image ResizeImage(Image srcImage, int width)
{
var b = new Bitmap(width, srcImage.Height * width / srcImage.Width);
using (var g = Graphics.FromImage((Image)b))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(srcImage, 0, 0, b.Width, b.Height);
}
return b;
}