本文整理汇总了C#中Bitmap.SetResolution方法的典型用法代码示例。如果您正苦于以下问题:C# Bitmap.SetResolution方法的具体用法?C# Bitmap.SetResolution怎么用?C# Bitmap.SetResolution使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Bitmap
的用法示例。
在下文中一共展示了Bitmap.SetResolution方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CropBitmapBetter
public Bitmap CropBitmapBetter(Bitmap bitmap, Rectangle rect)
{
// An empty bitmap which will hold the cropped image
var bmp = new Bitmap(rect.Width, rect.Height);
bitmap.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution);
Graphics g = Graphics.FromImage(bmp);
// Draw the given area (section) of the source image
// at location 0,0 on the empty bitmap (bmp)
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
g.Dispose();
return bmp;
//var bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
//bmp.SetResolution(300, 300);
//Graphics graphics = Graphics.FromImage(bmp);
//graphics.SmoothingMode = SmoothingMode.AntiAlias;
//graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
//graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
//graphics.DrawImage(bitmap, new Rectangle(0,0, rect.Width,rect.Height), rect.X,rect.Y,rect.Width,rect.Height,GraphicsUnit.Pixel);
//graphics.Dispose();
//return bmp;
}
示例2: resizeImage
// Returns the thumbnail image when an image object is passed in
public static Image resizeImage(Image ImageToResize, int newWidth, int maxHeight, bool onlyResizeIfWider)
{
try
{
// Prevent using images internal thumbnail
ImageToResize.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
ImageToResize.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
// Set new width if in bounds
if (onlyResizeIfWider)
{
if (ImageToResize.Width <= newWidth)
{
newWidth = ImageToResize.Width;
}
}
// Calculate new height
int newHeight = ImageToResize.Height * newWidth / ImageToResize.Width;
if (newHeight > maxHeight)
{
// Resize with height instead
newWidth = ImageToResize.Width * maxHeight / ImageToResize.Height;
newHeight = maxHeight;
}
// Create the new
if (newWidth > 120 || newHeight > 120)
{
Bitmap newImage = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
newImage.SetResolution(ImageToResize.HorizontalResolution, ImageToResize.VerticalResolution);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(ImageToResize, new Rectangle(0, 0, newWidth, newHeight));
}
return newImage;
}
else
{
Image resizedImage = ImageToResize.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
return resizedImage;
}
}
catch
{
return null;
}
finally
{
// Clear handle to original file so that we can overwrite it if necessary
ImageToResize.Dispose();
}
}
示例3: Redimensiona
public static MemoryStream Redimensiona(Stream imagen, int target, int resolucion)
{
System.Drawing.Image original = System.Drawing.Image.FromStream(imagen);
System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(imagen);
int targetH;
int targetW;
int alto= imgPhoto.Height;
int ancho = imgPhoto.Width;
if (alto > ancho)
{
if (alto > target)
{
ancho = sf.entero((ancho * target) / alto);
alto = target;
}
}
else
{
if (ancho > target)
{
alto = sf.entero((alto * target) / ancho);
ancho = target;
}
}
// No vamos a permitir que la imagen final sea más grande que la inicial
//targetH = targetH <= original.Height ? targetH : original.Height;
//targetW = targetW <= original.Width ? targetW : original.Width;
targetH = alto;
targetW = ancho;
Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
// No vamos a permitir dar una resolución mayor de la que tiene
resolucion = resolucion <= Convert.ToInt32(bmPhoto.HorizontalResolution) ? resolucion : Convert.ToInt32(bmPhoto.HorizontalResolution);
resolucion = resolucion <= Convert.ToInt32(bmPhoto.VerticalResolution) ? resolucion : Convert.ToInt32(bmPhoto.VerticalResolution);
bmPhoto.SetResolution(resolucion, resolucion);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), new Rectangle(0, 0, original.Width, original.Height), GraphicsUnit.Pixel);
MemoryStream mm = new MemoryStream();
bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
original.Dispose();
imgPhoto.Dispose();
bmPhoto.Dispose();
grPhoto.Dispose();
return mm;
}
示例4: FixedSize
public static Image FixedSize(Image imgPhoto, int Width, int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.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(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
// ͼƬ�ߴ粻��ʱ�������ɫ
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBilinear;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
示例5: Button1_Click
protected void Button1_Click(object sender, EventArgs e)
{
//gets dimensions
int SetWidth = int.Parse(txtWidth.Text), SetHeight = int.Parse(txtHeight.Text);
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToUpper();
System.Drawing.Image imgByte = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
int W = int.Parse(imgByte.PhysicalDimension.Width.ToString());
int H = int.Parse(imgByte.PhysicalDimension.Height.ToString());
//first checks if the photo is landscape
if (W > H)
{
//save paths defined here
string tempStorageFile = Server.MapPath(@"~/portraits/raw_72DPI/" + dateStampNoID + fileExtension);
string ConvertedSizeFile = Server.MapPath(@"~/portraits/ConvertedSize/" + dateStampNoID + fileExtension);
string FinalFile = Server.MapPath(@"~/portraits/FinalOut/" + dateStampNoID + fileExtension);
//makes sure na 72 DPI muna yung pic, then saves a temporary file on the portraits/raw_72DPI folder.
Bitmap temp = new Bitmap(FileUpload1.FileContent);
temp.SetResolution(72, 72);
temp.Save(tempStorageFile);
temp.Dispose();
//computes the new height to resize the image according to width set by user
double newHTD = (SetWidth * H) / W;
//then, Parses the double to int to remove decimals
int newH = int.Parse(newHTD.ToString());
//now, we resize the landscape image.... and saces to ConvertedSize Folder
convertpic(SetWidth, newH, W, H, tempStorageFile, ConvertedSizeFile);
//Then, We impose on a white background based on the portrait dimensions specified by the user
ImposeToPortrait(ConvertedSizeFile, FinalFile, SetWidth, SetHeight);
//displays file
Response.Write("Image done processing. Check portraits/FinalOut/ folder for file.");
}
else
{
Response.Write("Image is already portrait!");
}
}
示例6: Resize
public static Bitmap Resize(this Image image, Size size, InterpolationMode mode)
{
if (image == null || size.IsEmpty)
return null;
var resizedImage = new Bitmap(size.Width, size.Height, image.PixelFormat);
resizedImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var g = System.Drawing.Graphics.FromImage(resizedImage))
{
var location = new Point(0, 0);
g.InterpolationMode = mode;
g.DrawImage(image, new Rectangle(location, size), new Rectangle(location, image.Size), GraphicsUnit.Pixel);
}
return resizedImage;
}
示例7: Crop
private static byte[] Crop(string Img, int Width, int Height, int X, int Y)
{
using (var OriginalImage = new Bitmap(Img))
{
using (var bmp = new Bitmap(Width, Height, OriginalImage.PixelFormat))
{
bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
using (Graphics Graphic = Graphics.FromImage(bmp))
{
Graphic.SmoothingMode = SmoothingMode.AntiAlias;
Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
Graphic.DrawImage(OriginalImage, new Rectangle(0, 0, Width, Height), X, Y, Width, Height, GraphicsUnit.Pixel);
var ms = new MemoryStream();
bmp.Save(ms, OriginalImage.RawFormat);
return ms.GetBuffer();
}
}
}
}
示例8: ResizeImage
public Bitmap ResizeImage(Image imageToResize, int width, int height)
{
Bitmap resizedImage = null;
//a holder for the result
resizedImage = new Bitmap(width, height);
// set the resolutions the same to avoid cropping due to resolution differences
resizedImage.SetResolution(imageToResize.HorizontalResolution, imageToResize.VerticalResolution);
//use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(resizedImage))
{
//set the resize quality modes to high quality
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//draw the image into the target bitmap
graphics.DrawImage(imageToResize, 0, 0, resizedImage.Width, resizedImage.Height);
}
return resizedImage;
}
示例9: ResizeImageFile
public void ResizeImageFile(string imageFile, int targetSize)
{
Image original = Image.FromFile(imageFile);
int targetH, targetW;
if (original.Height > original.Width)
{
targetH = targetSize;
targetW = (int)(original.Width * ((float)targetSize / (float)original.Height));
}
else
{
targetW = targetSize;
targetH = (int)(original.Height * ((float)targetSize / (float)original.Width));
}
Image imgPhoto = Image.FromFile(imageFile);
// Create a new blank canvas. The resized image will be drawn on this canvas.
Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72, 72);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
// Save out to memory and then to a file. We dispose of all objects to make sure the files don't stay locked.
//
MemoryStream mm = new MemoryStream();
bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
original.Dispose();
imgPhoto.Dispose();
bmPhoto.Dispose();
grPhoto.Dispose();
File.Delete(imageFile);
FileStream fs = new FileStream(imageFile, FileMode.OpenOrCreate);
fs.Write(mm.GetBuffer(), 0, mm.GetBuffer().Length);
mm.Close();
fs.Close();
//return mm.GetBuffer();
}
示例10: resizeImageWithGivenValue
public Bitmap resizeImageWithGivenValue(System.Drawing.Image originalImage, int givenWidth, int givenHeight)
{
int sourceImageWidth = originalImage.Width;
int sourceImageHeight = originalImage.Height;
int distinationX = 0;
int distinationY = 0;
int sourceX = 0;
int sourceY = 0;
int resizedImageWidth = givenWidth;
int resizedImageHeight = givenHeight;
Bitmap resizedImage = new Bitmap(resizedImageWidth, resizedImageHeight, PixelFormat.Format24bppRgb);
resizedImage.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
Graphics newResizedImage = Graphics.FromImage(resizedImage);
newResizedImage.InterpolationMode = InterpolationMode.HighQualityBicubic;
newResizedImage.DrawImage(
originalImage,
new Rectangle(distinationX, distinationY, resizedImageWidth, resizedImageHeight),
new Rectangle(sourceX, sourceY, sourceImageWidth, sourceImageHeight),
GraphicsUnit.Pixel
);
newResizedImage.Dispose();
return resizedImage;
}
示例11: Resize
/// <summary>Imape
/// Resize image to specified size.
/// </summary>
/// <param name="image">Source image</param>
/// <param name="width">Required width</param>
/// <param name="height">Required heght</param>
/// <returns>Image</returns>
/// <summary>
/// Resize image to specified size.
/// </summary>
/// <param name="image">Source image</param>
/// <param name="width">Required width</param>
/// <param name="height">Required heght</param>
/// <returns>Image</returns>
public static Image Resize(Image image, int width, int height, bool crop, ImageType type)
{
if (image == null) throw new ArgumentNullException("image");
if (height == 0)
{
height = (int)(image.Height * width / (float)image.Width);
}
if (width == 0)
{
width = (int)(image.Width * height / (float)image.Height);
}
int sourceWidth = image.Width;
int sourceHeight = image.Height;
int destWidth, destHeight;
if (sourceWidth > sourceHeight) // Landscape
{
destWidth = width;
destHeight = (type==ImageType.EventPictureThumbnailSlideShow ? width : height) * sourceHeight / sourceWidth;
}
else //Portrait
{
destWidth = type == ImageType.EventPictureThumbnailSlideShow ? width / 2 : width;
destHeight = (type == ImageType.EventPictureThumbnailSlideShow ? width / 2 : height) * sourceHeight / sourceWidth;
}
Bitmap bitmap = new Bitmap(destWidth, destHeight,
PixelFormat.Format32bppArgb);
bitmap.SetResolution(image.HorizontalResolution,
image.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bitmap);
grPhoto.Clear(Color.Transparent);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
if(sourceWidth>sourceHeight) // Landscape
grPhoto.DrawImage(image,
new Rectangle(0, 0, destWidth, destHeight),
new Rectangle( 0, 0, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
else //Portrait
grPhoto.DrawImage(image,
new Rectangle(0, 0, destWidth, destHeight),
new Rectangle(0, 0, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bitmap;
}
示例12: Fit2Control
public static Image Fit2Control(Image image, Control container)
{
Bitmap bmp = null;
Graphics g;
// Scale:
double scaleY = (double)image.Width / container.Width;
double scaleX = (double)image.Height / container.Height;
double scale = scaleY < scaleX ? scaleX : scaleY;
// Create new bitmap:
bmp = new Bitmap(
(int)((double)image.Width / scale),
(int)((double)image.Height / scale));
// Set resolution of the new image:
bmp.SetResolution(
image.HorizontalResolution,
image.VerticalResolution);
// Create graphics:
g = Graphics.FromImage(bmp);
// Set interpolation mode:
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the new image:
g.DrawImage(
image,
new Rectangle( // Destination
0, 0,
bmp.Width, bmp.Height),
new Rectangle( // Source
0, 0,
image.Width, image.Height),
GraphicsUnit.Pixel);
// Release the resources of the graphics:
g.Dispose();
// Release the resources of the origin image:
image.Dispose();
return bmp;
}
示例13: getimage
public static bool getimage(string pimage, string foldername, int large, int medium, int small)
{
int imgwidth = 0;
int imgheight = 0;
int imgthumbwidth = 0;
int imgthumbheight = 0;
int imgiconwidth = 0;
int imgiconheight = 0;
//Panel1.Visible = true;
System.Drawing.Image.GetThumbnailImageAbort callback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallBack);
System.Drawing.Image mainImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("\\userfiles\\" + foldername + "\\") + pimage);
//--resize main image - if image's height or width > 250 px
if (mainImage.Width >= large || mainImage.Height >= large)
{
if (mainImage.Width > mainImage.Height)
{
imgwidth = large;
imgheight = (mainImage.Height * large) / mainImage.Width;
imgthumbwidth = medium;
imgthumbheight = (mainImage.Height * medium) / mainImage.Width;
imgiconwidth = small;
imgiconheight = (mainImage.Height * small) / mainImage.Width;
}
else if (mainImage.Width < mainImage.Height)
{
imgheight = large;
imgwidth = (mainImage.Width * large) / mainImage.Height;
imgthumbheight = medium;
imgthumbwidth = (mainImage.Width * medium) / mainImage.Height;
imgiconheight = small;
imgiconwidth = (mainImage.Width * small) / mainImage.Height;
}
else if (mainImage.Width == mainImage.Height)
{
imgheight = large;
imgwidth = large;
imgthumbwidth = medium;
imgthumbheight = medium;
imgiconheight = small;
imgiconwidth = small;
}
string ProductImageMain = pimage;
Bitmap thumbnail1 = new Bitmap(imgwidth, imgheight, PixelFormat.Format24bppRgb);
thumbnail1.SetResolution(mainImage.HorizontalResolution, mainImage.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(thumbnail1);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
grPhoto.CompositingQuality = CompositingQuality.HighQuality;
grPhoto.DrawImage(mainImage,
new Rectangle(-1, -1, imgwidth + 2, imgheight + 2),
new Rectangle(-1, -1, mainImage.Width + 2, mainImage.Height + 2),
GraphicsUnit.Pixel);
grPhoto.Dispose();
thumbnail1.Save(HttpContext.Current.Server.MapPath("\\userfiles\\" + foldername + "\\large\\") + ProductImageMain);
thumbnail1.Dispose();
string ProudctImageThumb = pimage;
Bitmap thumbnail2 = new Bitmap(imgthumbwidth, imgthumbheight, PixelFormat.Format24bppRgb);
thumbnail2.SetResolution(mainImage.HorizontalResolution, mainImage.VerticalResolution);
Graphics grPhoto2 = Graphics.FromImage(thumbnail2);
grPhoto2.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto2.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto2.PixelOffsetMode = PixelOffsetMode.HighQuality;
grPhoto2.CompositingQuality = CompositingQuality.HighQuality;
grPhoto2.DrawImage(mainImage,
new Rectangle(-1, -1, imgthumbwidth + 2, imgthumbheight + 2),
new Rectangle(-1, -1, mainImage.Width + 2, mainImage.Height + 2),
GraphicsUnit.Pixel);
grPhoto2.Dispose();
thumbnail2.Save(HttpContext.Current.Server.MapPath("\\userfiles\\" + foldername + "\\medium\\") + ProudctImageThumb);
thumbnail2.Dispose();
//--create icon of the image
string ProductImageIcon = pimage;
Bitmap thumbnail3 = new Bitmap(imgiconwidth, imgiconheight, PixelFormat.Format24bppRgb);
thumbnail3.SetResolution(mainImage.HorizontalResolution, mainImage.VerticalResolution);
Graphics grPhoto3 = Graphics.FromImage(thumbnail3);
grPhoto3.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto3.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto3.PixelOffsetMode = PixelOffsetMode.HighQuality;
grPhoto3.CompositingQuality = CompositingQuality.HighQuality;
grPhoto3.DrawImage(mainImage,
new Rectangle(-1, -1, imgiconwidth + 2, imgiconheight + 2),
new Rectangle(-1, -1, mainImage.Width + 2, mainImage.Height + 2),
GraphicsUnit.Pixel);
grPhoto3.Dispose();
thumbnail3.Save(HttpContext.Current.Server.MapPath("\\userfiles\\" + foldername + "\\small\\") + ProductImageIcon);
//.........这里部分代码省略.........
示例14: Code39_createCode
private Bitmap Code39_createCode(float resolution)
{
int num6;
int finalLength = 0;
char[] chArray = this._bitpattern_c39(DataToEncode, ref finalLength);
if (chArray == null)
{
return null;
}
float fontsize = this._humanReadable ? (0.3527778f * this._humanReadableSize) : 0f;
// float num3 = (7f * ModuleWidth) + ((3f * Ratio) * ModuleWidth);
float num3 = (7f * this._moduleWidth) + ((3f * this._ratio) * this._moduleWidth);
float width = (finalLength * num3) + (2f * this._marginX);
float height = (this._moduleHeight + (2f * this._marginY)) + fontsize;
width *= resolution / 25.4f;
height *= resolution / 25.4f;
Bitmap image = new Bitmap((int)width, (int)height, PixelFormat.Format32bppPArgb);
image.SetResolution(resolution, resolution);
//image.SetResolution(300, 300);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.White);
g.PageUnit = GraphicsUnit.Millimeter; //以毫米为度量单位
g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, /*(int)width*/image.Width, /*(int)height*/image.Height));
//new Pen(Color.Black, 2f);
//new SolidBrush(Color.White);
SolidBrush brush = new SolidBrush(Color.Black);
if (resolution < 300f)
{
//g.TextRenderingHint = TextRenderingHint.AntiAlias;
//g.SmoothingMode = SmoothingMode.AntiAlias;
g.CompositingQuality = CompositingQuality.HighQuality;
//g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
}
float num7 = 0f;
for (num6 = 0; num6 < chArray.Length; num6++)
{
if (chArray[num6] == '0')
{
if ((num6 & 1) != 1)
{
RectangleF rect = new RectangleF(MarginX + num7, MarginY, ModuleWidth, ModuleHeight);
MakeBar(g, rect, Reduction);
}
num7 += 1f * ModuleWidth;
}
else
{
if ((num6 & 1) != 1)
{
RectangleF ef2 = new RectangleF(MarginX + num7, MarginY, Ratio * ModuleWidth, ModuleHeight);
MakeBar(g, ef2, Reduction);
}
num7 += Ratio * ModuleWidth;
}
}
#region 供人识别内容
if (this._humanReadable)
{
#region 保留
/*byte[] buffer2 = processTilde(this._dataToEncode);
int index = 0;
List<byte> arr = new List<byte>();
for (num6 = 0; num6 < buffer2.Length; num6++)
{
//0x20 32 0x7e 126
if ((buffer2[num6] >= 0x20) && (buffer2[num6] <= 0x7e))
{
arr.Add(buffer2[num6]);
}
}
byte[] bytes = new byte[arr.Count];
for (int i = 0; i < arr.Count; i++)
{
bytes[i] = arr[i];
}
index = arr.Count;
//string text = new ASCIIEncoding().GetString(bytes, 0, index);
*/
#endregion
string text = this._dataToEncode;
if (this._isDisplayCheckCode && !string.IsNullOrEmpty(this._checkData))
{
text += this._checkData;
}
if (this._isDisplayStartStopSign)
{
text = "*" + text + "*";
}
Font font = new Font(this._humanReadableFont, this._humanReadableSize);
//g.DrawString(text, font, brush, new PointF(MarginX, MarginY + ModuleHeight));
//新增字符串格式
var drawFormat = new StringFormat { Alignment = StringAlignment.Center };
float inpix = image.Width / resolution;//根据DPi求出 英寸数
float widthmm = inpix * 25.4f; //有每英寸像素求出毫米
//g.DrawString(text, font, Brushes.Black, width / 2, height - 14, drawFormat);
g.DrawString(text, font, /*Brushes.Black*/brush, new PointF(/*MarginX*/(int)(widthmm / 2), MarginY + ModuleHeight + 1), drawFormat);
}
//.........这里部分代码省略.........
示例15: GenWatermark
private byte[] GenWatermark(byte[] buffer, ImageFormat format)
{
string Copyright = "Copyright © kermanshahchhto.ir";
MemoryStream pMS = new MemoryStream(buffer);
System.Drawing.Image imgPhoto = new System.Drawing.Bitmap(pMS);
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, imgPhoto.PixelFormat);
//bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
bmPhoto.SetResolution(72, 72);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.DrawImage(
imgPhoto,
new Rectangle(0, 0, phWidth, phHeight),
0,
0,
phWidth,
phHeight,
GraphicsUnit.Pixel);
int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };
Font crFont = null;
SizeF crSize = new SizeF();
for (int i = 0; i < 7; i++)
{
//crFont = new Font("arial", sizes[i], FontStyle.Bold);
crFont = new Font("Verdana", sizes[i], FontStyle.Bold);
crSize = grPhoto.MeasureString(Copyright, crFont);
if ((ushort)crSize.Width < (ushort)phWidth)
break;
}
int yPixlesFromBottom = (int)(phHeight * .05);
float yPosFromBottom = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2));
float xCenterOfImg = (phWidth / 2);
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
grPhoto.DrawString(Copyright,
crFont,
semiTransBrush2,
new PointF(xCenterOfImg + 1, yPosFromBottom + 1),
StrFormat);
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
grPhoto.DrawString(Copyright,
crFont,
semiTransBrush,
new PointF(xCenterOfImg, yPosFromBottom),
StrFormat);
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
imgPhoto = bmWatermark;
grPhoto.Dispose();
MemoryStream wMS = new MemoryStream();
imgPhoto.Save(wMS, format);
imgPhoto.Dispose();
Array.Resize(ref buffer, 0);
buffer = wMS.ToArray();
return buffer;
}